Promise.all: Order of resolved values Promise.all: Order of resolved values javascript javascript

Promise.all: Order of resolved values


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 resultsconst write = msg => {  document.body.appendChild(document.createElement('div')).innerHTML = msg;};// Different speed async operationsconst 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 firstPromise.all([slow, instant, quick]).then(responses => {  responses.map(response => write(response));});