---
title: "How do I cancel a fetch request or add a timeout in JavaScript?"
description: "Use AbortController to cancel a fetch, AbortSignal.timeout to give up after a set time, and AbortSignal.any to combine both. Here is how each one works."
url: https://www.danny.engineering/answer/javascript/how-do-i-cancel-a-fetch-request-or-add-a-timeout-in-javascript
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Javascript"]
---

# How do I cancel a fetch request or add a timeout in JavaScript?

## The short answer


`fetch` has no timeout option, but it accepts a `signal`. Pass `AbortSignal.timeout(ms)` to give up after a set time, or create an `AbortController` and call `abort()` to cancel on demand. When the signal fires, the `fetch` promise rejects with a `DOMException` you can check by name.


```javascript
try {
  const response = await fetch('/api/report', {
    signal: AbortSignal.timeout(5000),
  });
  const data = await response.json();
} catch (error) {
  if (error.name === 'TimeoutError') {
    console.warn('The request took longer than 5 seconds');
  } else {
    throw error;
  }
}
```


## Why it happens


A promise on its own cannot be cancelled. The platform solves this with abort signals: an `AbortSignal` is an object that can switch once from "not aborted" to "aborted", and APIs that accept one stop their work when that happens. `fetch` closes the request, and the promise rejects.


The name of the error tells you why it stopped:

- `AbortError` when you called `controller.abort()`.
- `TimeoutError` when a signal from `AbortSignal.timeout()` expired.

The same signals work with other APIs too, including `addEventListener`, streams, and many Node.js functions.


## How to fix it


### Cancel on demand with AbortController


Create a controller, pass its `signal` to `fetch`, and call `abort()` when the result is no longer needed, such as when the user starts a new search.


```javascript
let controller;

async function search(query) {
  controller?.abort();
  controller = new AbortController();

  try {
    const response = await fetch(`/api/search?q=${encodeURIComponent(query)}`, {
      signal: controller.signal,
    });
    renderResults(await response.json());
  } catch (error) {
    if (error.name !== 'AbortError') throw error;
  }
}
```


This stops stale responses from an earlier search overwriting newer results.


### Add a timeout with AbortSignal.timeout


`AbortSignal.timeout(ms)` returns a signal that aborts itself after the given number of milliseconds. You do not need to create a controller or clear a timer.


```javascript
const response = await fetch(url, { signal: AbortSignal.timeout(8000) });
```


### Combine a timeout and manual cancel with AbortSignal.any


`AbortSignal.any()` takes a list of signals and returns one that aborts when any of them does. Its `reason` comes from the signal that fired first.


```javascript
const controller = new AbortController();
cancelButton.addEventListener('click', () => controller.abort());

const signal = AbortSignal.any([
  controller.signal,
  AbortSignal.timeout(10000),
]);

const response = await fetch('/api/export', { signal });
```


`AbortSignal.timeout` and `AbortSignal.any` are both supported in all current major browsers and in current Node.js LTS releases. `AbortSignal.any` became Baseline in 2024, so check your support targets if you need older browsers.


### Cancel inside your own async functions


Pass the signal through so callers can stop your code as well as the request.


```javascript
async function loadAll(urls, { signal } = {}) {
  const results = [];
  for (const url of urls) {
    signal?.throwIfAborted();
    const response = await fetch(url, { signal });
    results.push(await response.json());
  }
  return results;
}
```


## Common pitfalls

- Reusing a controller after `abort()`. A signal stays aborted for good, so create a new controller for each request.
- Treating every abort as a failure. Check `error.name` and ignore `AbortError` when you cancelled on purpose.
- Building a timeout with `Promise.race`. The request carries on running in the background; a signal actually stops it.
- Forgetting that the timeout covers the body too. If the signal fires while you are reading `response.json()`, that call rejects.
- Aborting in a React effect cleanup and showing an error message. That abort is expected; skip the error state for it.

## Related reading

- [MDN: AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)
- [MDN: AbortSignal.timeout()](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout_static)
- [MDN: AbortSignal.any()](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static)
