306

Looking at MDN it looks like the values passed to the then() callback of Promise.all contains the values in the order of the promises. For example:

var somePromises = [1, 2, 3, 4, 5].map(Promise.resolve);
return Promise.all(somePromises).then(function(results) {
  console.log(results) //  is [1, 2, 3, 4, 5] the guaranteed result?
});

Can anybody quote a spec stating in which order values should be in?

PS: Running code like that showed that this seems to be true although that is of course no proof - it could have been coincidence.

3 Answers 3

409

Shortly, the order is preserved.

Following the spec you linked to, Promise.all(iterable) takes an iterable as a parameter and internally calls PerformPromiseAll(iterator, constructor, resultCapability) with it, where the latter loops over iterable using IteratorStep(iterator).

Resolving is implemented via Promise.all() Resolve where each resolved promise has an internal [[Index]] slot, which marks the index of the promise in the original input.


All this means the output is strictly ordered given the iterable you pass to Promise.all() is strictly ordered (for example, an array).

You can see this in action in the below fiddle (ES6):

// Used to display results
const write = msg => {
  document.body.appendChild(document.createElement('div')).innerHTML = msg;
};

// Different speed async operations
const slow = new Promise(resolve => {
  setTimeout(resolve, 200, 'slow');
});
const instant = 'instant';
const quick = new Promise(resolve => {
  setTimeout(resolve, 50, 'quick');
});

// The order is preserved regardless of what resolved first
Promise.all([slow, instant, quick]).then(responses => {
  responses.map(response => write(response));
});

9
  • 2
    How would an iterable not be strictly ordered? Any iterable is "strictly ordered" by the order it produces its values in. Jan 21, 2015 at 12:37
  • 1
    Note - Firefox is the only browser implementing iterables in promises correctly. Chrome will currently throw an excpetion if you pass an iterable to Promise.all. In addition I'm not aware of any userland promise implementation that currently supports passing iterables although many have debated it and decided against it at the time. Jan 21, 2015 at 12:41
  • 3
    @BenjaminGruenbaum Isn't it possible to have an iterable that produces two different orders upon being iterated twice? For example, a deck of cards that produces cards in random order when it is iterated? I don't know if "strictly ordered" is the right terminology here, but not all iterables have a fixed order. So I think it's reasonable to say that iterators are "strictly ordered" (assuming that's the right term), but iterables are not.
    – JLRishe
    Jan 21, 2015 at 16:05
  • 4
    @JLRishe I guess you're right, it is indeed iterators that are ordered - iterables are not. Jan 21, 2015 at 17:38
  • 23
    It's worth noting that the promises don't chain. While you'll get the resolution in the same order, there's no guarantee about when the promises are acted on. In other words, Promise.all can't be used to run an array of promises in order, one after the other. The promises loaded into the iterator need to be independent of each other for this to work predictably. Apr 9, 2015 at 1:28
110

As the previous answers have already stated, Promise.all aggregates all resolved values with an array corresponding to the input order of the original Promises (see Aggregating Promises).

However, I would like to point out, that the order is only preserved on the client side!

To the developer it looks like the Promises were fulfilled in order but in reality, the Promises are processed at different speeds. This is important to know when you work with a remote backend because the backend might receive your Promises in a different order.

Here is an example that demonstrates the issue by using timeouts:

Promise.all

const myPromises = [
  new Promise((resolve) => setTimeout(() => {resolve('A (slow)'); console.log('A (slow)')}, 1000)),
  new Promise((resolve) => setTimeout(() => {resolve('B (slower)'); console.log('B (slower)')}, 2000)),
  new Promise((resolve) => setTimeout(() => {resolve('C (fast)'); console.log('C (fast)')}, 10))
];

Promise.all(myPromises).then(console.log)

In the code shown above, three Promises (A, B, C) are given to Promise.all. The three Promises execute at different speeds (C being the fastest and B being the slowest). That's why the console.log statements of the Promises show up in this order:

C (fast) 
A (slow)
B (slower)

If the Promises are AJAX calls, then a remote backend will receive these values in this order. But on the client side Promise.all ensures that the results are ordered according to the original positions of the myPromises array. That's why the final result is:

['A (slow)', 'B (slower)', 'C (fast)']

If you want to guarantee also the actual execution of your Promises, then you would need a concept like a Promise queue. Here is an example using p-queue (be careful, you need to wrap all Promises in functions):

Sequential Promise Queue

const PQueue = require('p-queue');
const queue = new PQueue({concurrency: 1});

// Thunked Promises:
const myPromises = [
  () => new Promise((resolve) => setTimeout(() => {
    resolve('A (slow)');
    console.log('A (slow)');
  }, 1000)),
  () => new Promise((resolve) => setTimeout(() => {
    resolve('B (slower)');
    console.log('B (slower)');
  }, 2000)),
  () => new Promise((resolve) => setTimeout(() => {
    resolve('C (fast)');
    console.log('C (fast)');
  }, 10))
];

queue.addAll(myPromises).then(console.log);

Result

A (slow)
B (slower)
C (fast)

['A (slow)', 'B (slower)', 'C (fast)']
4
  • 7
    great answer, expecially using PQueue
    – ironstein
    Mar 8, 2018 at 6:38
  • I need a Sequential Promise Queue, but how do it if I must to do it from a result sql records? in a for ? while ?, no alternative in ES2017 our ES2018?
    – stackdave
    Dec 7, 2018 at 7:40
  • PQueue helped me! Thank you! :)
    – podeig
    Jul 2, 2020 at 8:55
  • 1
    What's differences between this and for-of-loop with await each promise in order? Aug 23, 2021 at 23:29
35

Yes, the values in results are in the same order as the promises.

One might cite the ES6 spec on Promise.all, though it's a bit convoluted due to the used iterator api and generic promise constructor. However, you'll notice that each resolver callback has an [[index]] attribute which is created in the promise-array iteration and used for setting the values on the result array.

6
  • Weird , I saw a youtube video today that said that the output order is determined by the first who resolved ,then second, then.....I guess the video OP was wrong?
    – Royi Namir
    Nov 15, 2015 at 8:10
  • 2
    @RoyiNamir: Apparently he was.
    – Bergi
    Nov 15, 2015 at 12:45
  • @Ozil Wat? The chronological order of resolution does absolutely not matter when all promises fulfill. The order of values in the result array is the same as in the input array of promises. If it's not, you should switch to a proper promise implementation.
    – Bergi
    Sep 21, 2017 at 14:12
  • Hi@Bergi +1. It means that the individual promises can be resolved in any order, (which doesn't matter because the purpose of all() is to get promises in parallel), what actually matters is that it returns an array with the results in the same order; they just won't be resolved in order, right? THANKS IN ADVANCE
    – user19481364
    Jan 31 at 14:28
  • 1
    @Coder23 The promises passed to Promise.all resolve whenever they want, by themselves, in any order. Promise.all does not, and can not, control them. They would resolve even if they were not passed to Promise.all at all.
    – Bergi
    Jan 31 at 21:37

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.