Using Q promise to receive different apis values

1

I need to receive some APIS values. For example: calls to api1() and api2() return deferred.promise ;

I can:

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()) ...

    
asked by anonymous 29.06.2016 / 22:29

1 answer

2

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

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

It expects all QPromises to be resolved, and then continues to pass the res in the example an array with the corresponding values of resolve of each initial QPromise.

    
29.06.2016 / 22:48