Promise.race: First to Settle Wins
Learn how Promise.race settles with the first promise to finish, use it for timeouts, and see why it does not cancel the losers or ignore early failures.

Promise.race returns a promise that copies the outcome of whichever input settles first, fulfilled or rejected. That "or rejected" is the part people forget, and it is what separates race from Promise.any. The classic use is a timeout: race a slow operation against a timer, and whichever finishes first decides the result. This post covers the semantics on Node.js 24 with printed output, builds a correct timeout, shows what race does not do (it cancels nothing and cleans nothing up) and lists the traps, including the empty array that never settles. For a more modern timeout, AbortSignal.timeout is usually better, and it gets its own post.
The behavior
The combined promise settles when the first input settles, with the same value or reason.
const delay = (ms, value) =>
new Promise((resolve) => setTimeout(() => resolve(value), ms));
const winner = await Promise.race([
delay(30, "slow"),
delay(10, "fast"),
delay(20, "medium"),
]);
console.log(winner);fastThe other two still ran to completion, and their results were discarded. race does not stop them.
A rejection can win
If the first input to settle is a rejection, the race rejects.
const delay = (ms, value) =>
new Promise((resolve) => setTimeout(() => resolve(value), ms));
const failAfter = (ms, message) =>
new Promise((_, reject) => setTimeout(() => reject(new Error(message)), ms));
try {
await Promise.race([delay(30, "a good answer"), failAfter(10, "quick failure")]);
} catch (error) {
console.log("race rejected:", error.message);
}race rejected: quick failureA fast failure beat a slow success. If you want the first success and are willing to wait through failures, use [Promise.any](/library/promise-any-first-succeed-wins) instead.
Plain values win immediately
A non-promise value counts as already settled, so it wins unless an earlier input is also settled. In practice, that makes a plain value in the list win on the next microtask.
const delay = (ms, value) =>
new Promise((resolve) => setTimeout(() => resolve(value), ms));
console.log(await Promise.race([delay(10, "timer"), "plain value"]));
console.log(await Promise.race([Promise.resolve("first"), Promise.resolve("second")]));plain value
firstWhen two inputs are already settled, the one earlier in the array wins, because its handlers were attached first.
Building a timeout
The standard use: race the real work against a promise that rejects after a delay.
function withTimeout(promise, ms) {
let timer;
const timeout = new Promise((_, reject) => {
timer = setTimeout(() => reject(new Error(`timed out after ${ms} ms`)), ms);
});
return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
}
const slow = new Promise((resolve) => setTimeout(() => resolve("data"), 100));
const quick = new Promise((resolve) => setTimeout(() => resolve("data"), 10));
console.log(await withTimeout(quick, 50));
try {
await withTimeout(slow, 50);
} catch (error) {
console.log(error.message);
}data
timed out after 50 msThe finally clears the timer. Without it, a successful result leaves a pending timer that keeps the Node.js process alive until it fires, and the late rejection is never handled by anyone because the race already settled. Clearing the timer removes both problems.
What race does not do
It does not cancel the loser. In the timeout above, the slow operation continues after the timeout error. A request keeps downloading, a query keeps running, a file keeps being written. To actually stop it, pass an AbortSignal to the work.
async function fetchLike(signal) {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => resolve("finished anyway"), 100);
signal.addEventListener("abort", () => {
clearTimeout(timer);
reject(signal.reason);
});
});
}
const controller = new AbortController();
const work = fetchLike(controller.signal);
setTimeout(() => controller.abort(new Error("gave up")), 20);
try {
await work;
} catch (error) {
console.log(error.message);
}gave upHere the work itself stopped and released its timer, which a bare race cannot do.
It does not collect results. Everything except the winner is thrown away, so do not use it where you need all answers. Use Promise.all or allSettled.
It does not guard against slow rejections you never see. If a loser rejects after the race settles, the rejection is still handled by race internally, so it does not crash the process as an unhandled rejection. The cost is only wasted work.
Pattern: the fastest of several sources
When several equivalent sources can answer, take the first. This works when a fast failure is acceptable or handled elsewhere.
const delay = (ms, value) =>
new Promise((resolve) => setTimeout(() => resolve(value), ms));
const mirrors = [delay(40, "mirror-a"), delay(15, "mirror-b"), delay(25, "mirror-c")];
console.log("served by", await Promise.race(mirrors));served by mirror-bFor this job a failed mirror would win the race and break the request, so in practice Promise.any is the safer choice, with race reserved for timeouts where a failure should win.
Pattern: waiting for an event or a deadline
import { EventEmitter, once } from "node:events";
const emitter = new EventEmitter();
setTimeout(() => emitter.emit("ready", "payload"), 10);
const deadline = new Promise((_, reject) =>
setTimeout(() => reject(new Error("deadline passed")), 200),
);
const [payload] = await Promise.race([once(emitter, "ready"), deadline]);
console.log(payload);payloadThe once helper returns a promise for the event's arguments as an array, so the destructuring takes the first one. The leftover 200 ms timer keeps the process alive for a moment, which is the same cleanup issue as before.
Traps
An empty array never settles. Promise.race([]) returns a promise that stays pending forever, and await on it hangs the function silently.
const forever = Promise.race([]);
const result = await Promise.race([
forever,
new Promise((resolve) => setTimeout(() => resolve("gave up waiting"), 20)),
]);
console.log(result);gave up waitingIf the list comes from data, check its length first.
Treating a timeout error like a cancel. After the race rejects, the work is still running. Clean it up deliberately.
Racing in a loop. A race inside a loop creates timers per iteration; clear each one, or use AbortSignal.timeout, which handles the timer for you.
Timeouts in practice
A timeout is a decision about how long the caller is willing to wait, and it is separate from how long the work takes. That separation is why the race pattern works and also why it is incomplete. The caller stops waiting, but the work continues, consuming a socket, a database connection or a CPU slice until it ends on its own.
For network calls, the better design is to give the operation a deadline it understands. Modern fetch accepts a signal, and AbortSignal.timeout(ms) creates one that aborts itself. When the deadline passes, the request is cancelled, the connection is released and the promise rejects with a TimeoutError. You write less code and leak nothing. Keep the race version for operations that cannot be aborted, such as a call into a library that offers no signal, and accept that the work may finish unseen.
Choose timeout values from observed behavior, not guesses. If a service answers in under a second on a normal day, a five second timeout lets you survive a bad day without hanging a user, and a thirty second timeout leaves them staring at a spinner. Log each timeout with the operation name so you can see which dependency causes them.
Also consider what the caller does next. A timeout that triggers a retry can multiply load on a struggling service, so pair it with a limit on attempts and a delay that grows between them. A timeout that triggers a fallback, such as cached data, is often the best outcome, because the user gets an answer and the failing service gets room to recover.
Testing a timeout built on race
A timeout helper has three behaviors to pin down. When the work finishes first, the result is returned and no timer is left pending, which you can check by asserting that the process can exit, or by spying on clearTimeout. When the timer wins, the helper rejects with the documented error, and the test should assert on the error's name or code and not on its text. When the work fails before the timeout, the original error is what the caller sees. For all three, avoid real waiting. Use the test runner's fake timers to advance the clock, or pass a tiny delay such as five milliseconds and a work promise that you control by hand. Also test the case you hope never happens, a work promise that rejects after the timeout, and assert that it does not surface as an unhandled rejection, since the race has already settled.


