← Back to Node.js Course | Chapter 4: Async Programming | Lesson 4 of 7

Promise.all

Promise.all runs several promises at the same time and waits for all of them.

In this page:

  1. Promise.all
Syntax
javascript
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

javascript
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
  1. Awaiting independent calls one after another
  2. Not handling rejection of all
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.