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.
Best Answer
Shortly, the order is preserved.
Following the spec you linked to,
Promise.all(iterable)
takes aniterable
as a parameter and internally callsPerformPromiseAll(iterator, constructor, resultCapability)
with it, where the latter loops overiterable
usingIteratorStep(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):