Promise.race
Returns the result of the first promise to settle — successful or not.
Available since: ES2015
Syntax
Promise.race(promises)Parameters
| Parameter | Type | Description |
|---|---|---|
| promises | Iterable | An array of promises. |
Return value
PromiseA promise with the result or error of whichever settles first.
Examples
A timeout:
const slow = new Promise((resolve) => setTimeout(() => resolve('data'), 500));
const timeout = new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), 100));
try {
console.log(await Promise.race([slow, timeout]));
} catch (error) {
console.log(error.message);
}Output:
TimeoutNotes
The losing promises are not cancelled — they keep running, nobody just needs their result. To cancel requests use AbortController.
See also
- Promise.any — Returns the first successful result and skips errors.
- Promise.all — Waits for all promises in parallel.
- setTimeout — Runs a function once after a delay.
Related recipes
In the tutorial
- Several promises at once — Promise.all, Promise.allSettled, Promise.race and Promise.any in JavaScript: how to wait for several asynchronous actions at the same time.