Contents

Programming Fundamentals › Concurrency & Async

Promise.all, race and allSettled

Waiting on many promises at once with different failure behavior.

Also known as: Promise.all, Promise.race, Promise.allSettled, Promise.any

Promise combinators are built-in functions that take several promises and give back one, so you can wait on many at once. They differ in what they do when some succeed and some fail.

CombinatorResolves whenFails when
Promise.allall succeed, giving an array of results in orderthe first one fails (the rest are ignored)
Promise.allSettledall have finished, success or not, with a status for eachnever
Promise.racethe first one settles, with its result or errorthe first settles with an error
Promise.anythe first one succeedsall fail
// Run independent requests together
const [user, orders] = await Promise.all([getUser(id), getOrders(id)]);

// Collect every outcome, even failures
const results = await Promise.allSettled(urls.map(fetchJson));
const ok = results.filter(r => r.status === "fulfilled").map(r => r.value);

// A timeout via race
await Promise.race([fetchData(), timeout(5000)]);

// Use the first mirror that answers
const data = await Promise.any([fetch(a), fetch(b)]);

Choosing

  • All must succeed (loading a page’s data): all.
  • Best effort, report each result (checking many services): allSettled.
  • First answer wins: any. First settled, including failure (timeouts): race.

Gotchas

  • all rejects as soon as one fails, but the others keep running, because promises can’t be cancelled (cancellation).
  • Starting them together matters. await a(); await b(); runs one after the other. Create the promises first, then combine.
  • Too many at once can overwhelm a server. Limit concurrency for large lists.
  • Results from all and allSettled come in the input order, not the finishing order.
  • Always handle rejections, or they surface as unhandled errors.