Promise.all
Promise.all runs several promises at the same time and waits for all of them.
In this page:
Syntax
const results = await Promise.all([promise1, promise2, promise3]);
Promise.all
Promise.all takes an iterable of promises and resolves with an array of results in order, or rejects as soon as one rejects. Promise.allSettled waits for all and reports each outcome, Promise.race resolves with the first to settle and Promise.any with the first success.
Note:
Use allSettled when one failure should not cancel the rest.
Example: Promise.all
const wait = (ms, v, fail) => new Promise((res, rej) => setTimeout(() => (fail ? rej(new Error(v)) : res(v)), ms));
(async () => {
console.log(await Promise.all([wait(20, "a"), wait(5, "b"), wait(10, "c")]));
const r = await Promise.allSettled([wait(5, "ok"), wait(5, "bad", true)]);
console.log(r.map((x) => x.status));
console.log(await Promise.race([wait(20, "slow"), wait(5, "fast")]));
})();
// Output:
// [ 'a', 'b', 'c' ]
// [ 'fulfilled', 'rejected' ]
// fast
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
Common Mistakes
- Awaiting independent calls one after another
- Not handling rejection of all
- Assuming results arrive in completion order
Chapter Summary
- all resolves with ordered results
- It rejects on first failure
- allSettled reports every outcome
- race and any pick a winner
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: