Javascript Fundamentals · Promises & Async

Promise combinators

Promise.all, allSettled, race, and any

Promise combinators

You have several promises in flight and need one answer out of the batch: all of them, the first to settle, or the first to succeed. The choice matters most when one fails — some waits should keep going, some should stop.

A combinator takes many promises and returns one. Which you reach for depends on what should happen when they settle differently.

  • Promise.all fulfils with an array of every result, in input order, once all fulfil. It rejects as soon as the first promise rejects.
  • Promise.allSettled always fulfils, once every promise has settled, with an array of { status: 'fulfilled', value } or { status: 'rejected', reason } objects. Use it when one failure should not sink the batch.
  • Promise.race settles with the first promise to settle, whether it fulfils or rejects.
  • Promise.any fulfils with the first promise to fulfil, ignoring rejections. If every promise rejects, it rejects with an AggregateError.

Reach for Promise.all when every result is required and any failure should abort the batch; Promise.allSettled when you want a full report of what succeeded and what failed; Promise.race for a timeout or first-response-wins; and Promise.any when the first success is enough. Each returns a single promise, so you await one value and get back an array — or, for race and any, the winning value itself.

const [user, posts] = await Promise.all([getUser(id), getPosts(id)]);
const results = await Promise.allSettled([a, b, c]);

You are about to answer a short quiz on the Promise combinators.

  1. Promise.all rejects as soon as any one of its input promises rejects.
  2. Promise.___ fulfils with every result, in input order, once all the promises fulfil.
  3. Promise.allSettled rejects if one of its promises rejects.
  4. Promise.___ settles with the first promise to settle, fulfilled or rejected.
  5. Promise.any fulfils with the first promise to fulfil, ignoring rejections.
  6. If every promise passed to Promise.any rejects, it rejects with an ___ error.
  7. Promise.allSettled resolves with objects carrying a status of 'fulfilled' or 'rejected'.