Skip to content Skip to sidebar Skip to footer

How To Call A Function With A Promise

I'm fairly new to JS and I'm trying to understand the documentation in a npm package. The documentation is: client.projects.get(); // Promise I've read some documentation on Promi

Solution 1:

A Promise is async execution of the code.

You can get the value returned from that async code using .then method on a Promise. You will have to pass the callback function which handles the value returned.

client.projects.get().then(function(foo) {
// this foo is returned from client.projects.get() async operation
})

In case that async operation threw some Exception, you can catch those using a .catch on a promise.

client.projects.get().then(function(foo) {
    // this foo is returned from client.projects.get() async operation
}).catch(function(err) {
   // something went wrong while executing client.projects.get()
})

Solution 2:

client.projects.get(); will return a promise and not probably "what you are expecting".

What you should do is to call it like this:

client.projects.get().then((result) => {
    // do with `result` your logic
    console.log(result);
});

and then inside the callback passed as argument to the then function receive the result the response provides and use it according to your logic.


Solution 3:

Play around with:

client.projects.get().then(result => console.log(result))

You will notice when working with a promise that you will need to specify what to do with its result once it is ready.

An alternative where you are simply returning the result would be:

client.projects.get().then(res => res)

If there is an error, you'll also want to add a catch:

client.projects.get().then(res => res).catch(err => console.error(err))

This will log out an error if there is a failure or some sort.


Solution 4:

> p.then(onFulfilled[, onRejected]);

onFulfilled - A Function called if the Promise is fulfilled.

onRejected (Optional) - A Function called if the Promise is rejected.

p.then(function(value) {
   // fulfillment
 }, function(reason) {
  // rejection
 });

> try -> catch -> finally

p.then(function(data) { console.log("Play with your data.") })
   .catch(function(error) { console.log(error); })
   .finally(function() { console.log("Something need to do, no matters fail or sucess") });
  1. The finally() method can be useful if you want to do some processing or cleanup once the promise is settled, regardless of its outcome.
  2. The finally() method is very similar to calling .then(onFinally, onFinally)

For more details.


So, you can write your code like:

 client.projects.get().then(function(value) {
    // fulfillment
  }, function(reason) {
    // rejection
  });

Or

 client.projects.get()
   .then(function(data) { console.log("Play with your data.") })
   .catch(function(error) { console.log(error); })
   .finally(function() { console.log("Finally do something.") });

Solution 5:

A Promise object represents a value that may not be available yet but will be resolved at some point in the future. It allows you to write asynchronous code in a more synchronous fashion. You can get the result once the promise resolved or catch the error in case of promise reject(failure). In your case, you have to call the function like this:

  client.projects.get().then(function(result){
       console.log(result);
    }).catch(function(err) {
        // handle error
        console.log("something went wrong",err);
     });

Alternatively, You can also store promise into variable returned from the function call and get the result, like this:

var promise = client.projects.get();

promise.then(function(result){
   console.log(result);
}).catch(function(err) {
    // handle error
    console.log("something went wrong",err);
 });

Having assigned promises into a variable may not be a good choice but It is very useful when there is more than one function which returns a promise and we want to execute some code after all promises have resolved. Something like this:

var p1 = asyncFunction1();
var p2 = asyncFunction2();
Promise.all([p1,p2]).then(function(results){
 // do something with results
});

You can also check this nice blog on promises


Post a Comment for "How To Call A Function With A Promise"