Four static methods run many promises concurrently and combine the results — they differ only in when they settle and what counts as failure.
| Method | Fulfils when | Rejects when | Result |
|---|---|---|---|
Promise.all | All fulfil | Any rejects (fail-fast) | Array of values, in input order |
Promise.allSettled | All settle | Never | Array of {status, value \| reason} |
Promise.race | The first to settle fulfils | The first to settle rejects | That one value/reason |
Promise.any | The first to fulfil | All reject | Value, or AggregateError |
const ok = (ms, v) => new Promise((res) => setTimeout(() => res(v), ms));
const bad = (ms, msg) => new Promise((_, rej) => setTimeout(() => rej(new Error(msg)), ms));
console.log("all:", await Promise.all([ok(30, "a"), ok(10, "b"), 42]));
await Promise.all([ok(10, "a"), bad(5, "db down")]).catch((e) => console.log("all rejected:", e.message));
const settled = await Promise.allSettled([ok(5, "a"), bad(5, "nope")]);
console.log(settled.map((r) => (r.status === "fulfilled" ? r : { ...r, reason: r.reason.message })));
console.log("race:", await Promise.race([ok(20, "slow"), ok(5, "fast")]));
console.log("any:", await Promise.any([bad(5, "x"), ok(15, "first success")]));
try {
await Promise.any([bad(5, "e1"), bad(5, "e2")]);
} catch (e) {
console.log(e.constructor.name, e.errors.map((x) => x.message));
}all: [ 'a', 'b', 42 ]
all rejected: db down
[
{ status: 'fulfilled', value: 'a' },
{ status: 'rejected', reason: 'nope' }
]
race: fast
any: first success
AggregateError [ 'e1', 'e2' ]Pattern: timeout with race
const timeout = (ms) => new Promise((_, rej) => setTimeout(() => rej(new Error(`timed out after ${ms}ms`)), ms));
const slowFetch = new Promise((res) => setTimeout(() => res("data"), 50));
try {
await Promise.race([slowFetch, timeout(10)]);
} catch (e) {
console.log(e.message);
}timed out after 10msSequential vs parallel
const job = (id, log) => new Promise((res) => setTimeout(() => { log.push(id); res(id); }, 10 * (4 - id)));
const seq = [];
for (const id of [1, 2, 3]) await job(id, seq); // one after another
console.log("sequential finish order:", seq);
const par = [];
const results = await Promise.all([1, 2, 3].map((id) => job(id, par))); // all at once
console.log("parallel finish order:", par, "results:", results);sequential finish order: [ 1, 2, 3 ]
parallel finish order: [ 3, 2, 1 ] results: [ 1, 2, 3 ]Promise.all vs Promise.allSettled?
race vs any?