# Promise.race()

> The `Promise.race()` static JS method takes an iterable of `Promise` objects and returns one `Promise` object that settles like the first one to settle.

## How do I add a timeout to a promise with Promise.race()?

The static method that returns a `Promise` object that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise. This is useful for handling the first result in a set of concurrent operations.

- Topic: Promise
- Type: method
- Browser support: Chrome 32+, Chrome Android 32+, Edge 12+, Firefox 29+, Firefox Android 29+, Safari 8+, Safari iOS 8+

## Example

Wrong:

```js
const timeout = new Promise((_, reject) => {
  setTimeout(() => reject(new Error('Timeout')), 5000);
});
const result = await Promise.race([task(), timeout]);
```

Right:

```js
let timer;
const timeout = new Promise((_, reject) => {
  timer = setTimeout(() => reject(new Error('Timeout')), 5000);
});
const result = await Promise.race([task(), timeout])
  .finally(() => clearTimeout(timer));
```

Clearing the timer in `finally()` stops a pending timeout from firing after the task has already won the race.

## Common mistakes

- Assuming the losing promise is canceled, while it keeps running in the background.
- Leaving the timeout's `setTimeout()` running after the main task wins.

## Key points

- The timer used for a timeout is cleared after the race settles, so it does not fire later.
- `fetch()` timeouts use `AbortSignal.timeout()`, so the request is actually canceled, not just ignored.
- The race never gets an empty array, which leaves the promise pending forever.

## Interview questions

### Does `Promise.race()` cancel the slower promises?

No. Promises cannot be canceled; the other operations keep running and their results are ignored. Use an `AbortSignal` to stop them.

### What happens if I pass an empty array to `Promise.race()`?

The returned promise stays pending forever, because nothing can settle it.

## Related cards

- [Promise.any()](https://wonderium.dev/js/promise-any.md)
- [Promise.all()](https://wonderium.dev/js/promise-all.md)
- [AbortSignal.timeout()](https://wonderium.dev/js/abort-signal-timeout.md)
- [setTimeout()](https://wonderium.dev/js/set-timeout.md)
- [Promise](https://wonderium.dev/js/promise.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/promise-race
- Last updated: 2026-11-25
