Promise.all and ordered concurrent results
How it works
Promise.all(iterable) fulfills with an array of values in input order after every input fulfills. It accepts ordinary values as well as promises and rejects when an input rejects. Empty input fulfills with an empty array. It does not start functions passed as values or cancel ongoing work after rejection. Start the operations deliberately and use cancellation only if the underlying operations cooperate.
Example 1
let finish; const first = new Promise(r => { finish = r; });const second = Promise.resolve("B");Promise.all([first, second]).then(v => console.log(v.join(",")));finish("A");Expected output in the tested environment:
A,BOutput order follows the iterable, not settlement order.
Example 2
Promise.all([Promise.resolve(1), Promise.reject("no")]).catch(console.log);Expected output in the tested environment:
noOne rejection prevents the aggregate from fulfilling.
Common mistake
Promise.all([taskA, taskB]) does not call taskA or taskB. Invoke tasks to obtain their promises.
Practice
Use two externally controlled promises; settle the second first, then the first, and inspect result order.
Run this practice in the documented local environment. ByteMind module and asynchronous grader support is not yet verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Promise.all and ordered concurrent results — Knowledge Check
Reason about the concept and predict its behavior in the indicated environment.
Which order does Promise.all preserve in its fulfillment array?
What does Promise.all([]) fulfill with?
Does Promise.all automatically cancel other pending work on rejection?
For Promise.all and ordered concurrent results, what is the console output of example 1 in Node.js 22.16.0?
let finish; const first = new Promise(r => { finish = r; });const second = Promise.resolve("B");Promise.all([first, second]).then(v => console.log(v.join(",")));finish("A");For Promise.all and ordered concurrent results, what is the console output of example 2 in Node.js 22.16.0?
Promise.all([Promise.resolve(1), Promise.reject("no")]).catch(console.log);