Skip to the content

Lesson 71 of 296

Promise.all, allSettled, race and any

What you will learn

  • Four ways to combine several promises
  • What each returns and when it rejects
  • Timeouts with Promise.race
  • Choosing the right one
On this page

Promise.all: all must succeed

const [user, posts] = await Promise.all([getUser(), getPosts()]);
  • Waits for all promises to fulfil.
  • Returns an array of values in the same order as the input, not the order they finished.
  • If any one rejects, the whole result rejects immediately with that error ("fail fast"). The other operations keep running, but their results are ignored.

Use it when you need every result and one failure means the whole thing failed.

Promise.allSettled: wait for everything, never rejects

const results = await Promise.allSettled([
  sendEmail(a),
  sendEmail(b),
  sendEmail(c)
]);

Result:

[
  { status: "fulfilled", value: "sent" },
  { status: "rejected", reason: Error("Mailbox full") },
  { status: "fulfilled", value: "sent" }
]
  • Waits for every promise to settle.
  • Never rejects.
  • Each result says whether it succeeded.

Use it when operations are independent and you want to know which ones failed:

const failed = results.filter((r) => r.status === "rejected");

Promise.race: first to settle wins

const result = await Promise.race([promiseA, promiseB]);
  • Settles as soon as the first promise settles, fulfilled or rejected.

The classic use is a timeout:

function timeout(ms) {
  return new Promise((_, reject) =>
    setTimeout(() => reject(new Error("Timed out")), ms)
  );
}

const data = await Promise.race([fetch("/api/slow"), timeout(5000)]);

If the request takes more than 5 seconds, you get a "Timed out" error. (The request itself keeps going in the background. To really cancel a fetch, use an AbortController, or AbortSignal.timeout(5000) in modern browsers and Node.js.)

Promise.any: first success wins

const fastest = await Promise.any([
  fetch("https://mirror1.example.com/file"),
  fetch("https://mirror2.example.com/file")
]);
  • Fulfils with the first fulfilled promise. Ignores rejections unless all fail.
  • If all reject, it rejects with an AggregateError, which contains every error in its errors property.

Use it when you only need one success, such as trying several servers.

Summary

MethodFulfils whenRejects whenResult
Promise.allAll fulfilAny rejects (first one)Array of values, input order
Promise.allSettledAll settleNeverArray of { status, value / reason }
Promise.raceFirst settles, if fulfilledFirst settles, if rejectedOne value
Promise.anyFirst fulfilsAll rejectOne value, or AggregateError

Empty arrays

MethodWith []
Promise.all([])Fulfils immediately with []
Promise.allSettled([])Fulfils immediately with []
Promise.race([])Stays pending forever
Promise.any([])Rejects with AggregateError

Parallel vs sequence: timing

Suppose three requests take 300 ms, 500 ms and 200 ms.

ApproachTotal time
Three awaits one after another1000 ms
Promise.allAbout 500 ms (the slowest one)

Limiting concurrency

Promise.all starts everything at once. With 1,000 requests, this can overload your server or hit rate limits. A promise pool runs a limited number at a time, for example 5. You will build one in Part 6.

How to say it in an interview

"Promise.all waits for all and returns values in input order, but rejects as soon as any one rejects. allSettled waits for all and never rejects, giving a status for each, which suits independent tasks. race settles with the first promise to settle, success or failure, which is useful for timeouts. any fulfils with the first success and only rejects with an AggregateError if all fail. I use these to run independent work in parallel, and a concurrency limit when there are many tasks."

Key words

WordMeaning
Promise.allWait for all; fail fast on the first rejection
Promise.allSettledWait for all; report each result
Promise.raceFirst to settle wins
Promise.anyFirst to succeed wins
AggregateErrorAn error that contains several errors
AbortControllerA way to cancel requests such as fetch
Concurrency limitThe maximum number of tasks running at the same time

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.