1

I am using the Kriskowal Q library to handle promises.

I wrote the following function to wait until all promises have been resolved.

function multiplePromises(targetArray) {
    var promises = {};
    for (var i=0; i<targetArray.length; i++) {
        var promise = singlePromise(); // any async. function
        promises[i] = promise;
    };
    return Q.all(promises);
};

and I call it as follows:

multiplePromises(targetArray).then(
     function(success){
         console.log("success");
     },
     function(error){
        console.log("error", error);
     }
);

I was wondering however whether there is an order in which the promises are resolved (e.g. is it synchronous?). I.e. does the function wait to trigger the next promise i+1 until promise i is resolved? Or alternatively is it like with all other async. methods, that it actually fires all the single promises and just waits until they are all done?

If the second is the case, how would one rewrite this function to make sure that promise i+1 is ONLY triggered once promise i has been resolved?

Update: test

I did a test and put:

promises[i] = i;

to check whether it resolves sycnhronously and it seems the case. However, it could be just that my async function is fast enough to actually resolve it that quick. Does this seem right?

WJA
  • 6,676
  • 16
  • 85
  • 152

3 Answers3

1

there are several ways to achieve what you want

Minimal change to your code would be

function multiplePromises(targetArray) {
    var promises = [];
    var p = Promise.resolve(); // I don't know the Q equivalent of this
    for (var i=0; i<targetArray.length; i++) {
        p = p.then(function() {
            return singlePromise();
        });
        promises[i] = p;
    };
    return Q.all(promises);
};
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
0

The promises will be executed by Q in the order you declare them, but there's no way to ensure the return order will be the same. That is how async works.

If you want them to resolve in order, the only way I can think of is calling them one after the other instead of doing it in a batch.

This other response will provide more info and some solutions: https://stackoverflow.com/a/36795097/7705625

Community
  • 1
  • 1
0

Promises in theory could be resolved in order (and it could be easy to write an example in which they would) but you should never count on that.

The whole point of functions like Promise.all() (or Q.all() or Bluebird.all() etc.) is that if waits for all of the promises to get resolved, no matter what is the order of that, and as soon as they are all resolved then the promise that the Promise.all() itself returns gets resolved with an array of values that the original promises resolved to.

In that array you will get always the same order as the order of promises in the array that was an argument to Promise.all(). But the order in time in which the original promises were resolves is not known and is not relevant, as that will have no effect on the result of using Promise.all() whatsoever.

rsp
  • 107,747
  • 29
  • 201
  • 177
  • Yes, but in my case I really need to resolve it in order as the previous promise has an impact on the next promise. – WJA Mar 14 '17 at 11:43