Using Q Promise to receive different Apis values

Asked

Viewed 56 times

1

I need to get the values of some Apis. For example: calls to api1() and api2() return deferred.promise;

I can do:

api1().then( function(res1){
  api2().then( function(res2){
       console.log(res1, res2);
  })
});

How can I improve these calls to Apis using Q Promises?

I tried to use something like: Q.fcall(api1()).then(api2())...

  • Take a look here: http://answall.com/a/137828/129, I think that’s what you want. In your case it would be Q.all([api1(),api2()]).then(function(res){console.log(res[0],res[1]); });. Test and tell if it works with the api Q.js.

1 answer

2


I think the equivalent of Promise.all in the Q.js is Q.all, with the same functionality as the native API. So you can do:

Q.all([api1(), api2()]).then(function(res) {
    console.log(res[0], res[1]);
});

He expects all Qpromises to be solved and then continues to res in the example an array with the corresponding values of resolve of each initial Qpromise.

  • 1

    It worked. Thank you, Sergio!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.