Async
Promise.all, race, allSettled, any
Waiting for several async operations at once — by different rules.
Why it exists
Async operations rarely come alone: a page loads the profile and the feed, a cart loads every item's price, a service asks three mirrors. Waiting for them in turn is slow. These four functions wait for them all at once and differ only in the rule for when the combined promise finishes, and with what result.
Where it's used
all: load everything a page needs in parallel.allSettled: dashboards and bulk operations where partial success is normal.race: a timeout for a request — a race against a timer.any: the fastest of several mirrors or CDNs.
Syntax
await Promise.all([p1, p2]) // [v1, v2] or an error
await Promise.allSettled([p1, p2]) // [{ status, value | reason }, …]
await Promise.race([p1, p2]) // the first to finish
await Promise.any([p1, p2]) // the first to succeedExamples
const all = Promise.all([Promise.resolve(1), Promise.resolve(2)]);
all.then((values) => console.log(values)); // [1, 2]const later = (value, ms) => new Promise((r) => setTimeout(() => r(value), ms));
await Promise.all([later("slow", 30), later("fast", 5)]); // → ["slow", "fast"]
await Promise.race([later("slow", 30), later("fast", 5)]); // → "fast"all keeps the order, race takes the firstconst results = await Promise.allSettled([Promise.resolve(1), Promise.reject(new Error("no"))]);
results.map((r) => r.status); // → ["fulfilled", "rejected"]allSettled reports on eachawait Promise.any([Promise.reject(new Error("mirror 1")), Promise.resolve("mirror 2")]); // → "mirror 2"any skips errors until the first successResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
All four take an array (more precisely, any iterable) of promises and return one promise. The promises themselves are already running by then — the combinator only waits for them.
- `Promise.all` fulfils with an array of values in the order of the original array once all have fulfilled. If any rejects, it rejects right away with that error (the other results are lost).
- `Promise.allSettled` waits until all have finished and never rejects. The result is an array of reports,
{ status: "fulfilled", value }or{ status: "rejected", reason }. - `Promise.race` follows the fate of the first to finish — success or error.
- `Promise.any` fulfils with the value of the first to succeed; it rejects (
AggregateError) only if all reject.
For an empty array, all and allSettled fulfil with [] immediately, any rejects, and race never finishes.
Traps
How it differs from…
- Promises:
Promise The combinators work with existing promises; how a single promise works is in a separate entry.
async/awaitawait in a loop waits one at a time;
await Promise.all(...)waits for all at once.
When to pick something else
If operations depend on each other (get the user first, then their orders by user.id), they can't run in parallel — you need sequential await. And don't fire thousands of requests at once through Promise.all: the server may refuse — limit how many run at the same time.
Practice
Lessons where this appears
- The pizza is on its wayTale · Chapter 16 · Async
- Byte's pizzeriaWorkshop · Chapter 16 · Async
- The case of "0[object Promise]"Investigation · Chapter 16 · Async
- All the prices at onceLab · Chapter 16 · Async
- The Endless SpinnerBoss · Chapter 16 · Async
- A customer's ordersLab · Chapter 21 · The network: fetch and HTTP