# AbortSignal.timeout()

> The `AbortSignal.timeout()` static JS method returns an `AbortSignal` that is automatically aborted after a given time.

## How do I set a timeout for fetch in JavaScript?

The static method that creates a signal that aborts with a `TimeoutError` after the specified number of milliseconds. Often passed to `fetch()` to set a request timeout without creating an `AbortController`.

- Topic: Specials
- Type: method
- Browser support: Chrome 124+, Chrome Android 124+, Edge 124+, Firefox 100+, Firefox Android 100+, Safari 16+, Safari iOS 16+

## Example

Wrong:

```js
const controller = new AbortController();
setTimeout(() => controller.abort(), 5000);
const res = await fetch(url, { signal: controller.signal });
```

Right:

```js
try {
  const res = await fetch(url, { signal: AbortSignal.timeout(5000) });
} catch (err) {
  if (err.name === 'TimeoutError') showRetry();
}
```

`AbortSignal.timeout()` needs no manual timer and rejects with a 'TimeoutError', which is easy to tell apart from user aborts.

## Common mistakes

- Checking only for 'AbortError', while a timeout rejects with 'TimeoutError'.

## Key points

- The timeout uses `AbortSignal.timeout(ms)`, not `setTimeout` with `controller.abort()`.
- The `catch` handles 'TimeoutError' separately from other errors.

## Interview questions

### What error does AbortSignal.timeout() cause?

The fetch rejects with a `DOMException` named 'TimeoutError', not 'AbortError'.

### Does the timeout also cover reading the response body?

Yes, the signal stays attached, so reading the body with `res.json()` is aborted too if time runs out.

## Related cards

- [AbortSignal.any()](https://wonderium.dev/js/abort-signal-any.md)
- [AbortController](https://wonderium.dev/js/abort-controller.md)
- [fetch()](https://wonderium.dev/js/fetch.md)
- [setTimeout()](https://wonderium.dev/js/set-timeout.md)

---

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