Learn Promise.race and Promise.allSettled in JavaScript with runnable examples. Timeouts, first-to-finish wins, and collecting every outcome including failures.
Promise.race and Promise.allSettled complete the 'combinator' family alongside Promise.all and Promise.any. race settles with whichever input settles first — useful for timeouts and first-response-wins patterns. allSettled waits for everything and returns per-promise outcome objects, so a single failure doesn't discard the rest. Together they cover the cases Promise.all can't: early exit and partial-success reporting.
Returns a promise that settles as soon as any input settles — resolved or rejected. Common use: racing a real operation against a timeout promise to impose a maximum wait.
Waits for all input promises regardless of success or failure, then resolves with an array of { status: 'fulfilled'|'rejected', value|reason } objects. Never rejects itself.
A close cousin: resolves with the first fulfilled result, ignoring rejections. Only rejects if all inputs reject, with an AggregateError collecting every failure.
all: everything must succeed. allSettled: collect everything, success or not. race: whoever finishes first wins (good or bad). any: whoever succeeds first wins.
Use race for timeouts and 'first response wins' patterns. Use allSettled when you need to report on every operation regardless of outcome (dashboards, batch status). Use any when a single success is enough and you want to ignore failures.
const delay = (ms, val) =>
new Promise(r => setTimeout(() => r(val), ms));
const fail = (ms, msg) =>
new Promise((_, reject) => setTimeout(() => reject(new Error(msg)), ms));
(async () => {
// race: first to settle wins
const fastest = await Promise.race([
delay(200, "slow"),
delay(50, "fast"),
delay(100, "medium"),
]);
console.log("race winner:", fastest);
// race as timeout
try {
const result = await Promise.race([
delay(500, "real result"),
fail(200, "Timeout after 200ms"),
]);
console.log("got:", result);
} catch (e) {
console.error("error:", e.message);
}
// allSettled: every outcome
const outcomes = await Promise.allSettled([
delay(50, "ok"),
fail(100, "oops"),
delay(75, "also ok"),
]);
outcomes.forEach((o, i) => {
if (o.status === "fulfilled") console.log(`${i}: ✓ ${o.value}`);
else console.log(`${i}: ✗ ${o.reason.message}`);
});
})();
Open this example in the TryJS playground to edit and run the code instantly in your browser — no signup needed.
Promise.race settles with whichever promise settles first — resolved or rejected. Promise.any waits for the first fulfilled promise and ignores rejections until all reject. Use race for timeouts, any for 'first success wins'.
When you need to know the outcome of every promise regardless of success or failure — dashboards, batch-processing reports, retries that care about which subset failed. Unlike Promise.all, a single rejection doesn't discard everything.
Yes — race your real promise against a setTimeout-based promise that rejects after N ms. Whichever settles first wins. Note that losers keep running in the background; for true cancellation use AbortController.
No — it always fulfills, with an array of per-promise outcome objects. Each entry has { status: 'fulfilled', value } or { status: 'rejected', reason }. You inspect each result individually.