Create an AbortController, pass its signal to fetch(url, { signal }), and call controller.abort() to cancel. The fetch promise rejects with a DOMException named AbortError (or with the reason you pass to abort(reason)). Signals are a general cancellation primitive: they also remove event listeners, and can be combined with AbortSignal.timeout() and AbortSignal.any().
const controller = new AbortController();
fetch('/api/search?q=react', { signal: controller.signal })
.then((r) => r.json())
.then(render)
.catch((err) => {
if (err.name === 'AbortError') return; // cancelled on purpose
showError(err);
});
// later, e.g. user navigates away
controller.abort();
Aborting also cancels reading the body. If you abort while res.json() is still streaming, that promise rejects too.
Cancel the previous request (search-as-you-type):
let current;
async function search(q) {
current?.abort();
current = new AbortController();
const res = await fetch(`/api/search?q=${encodeURIComponent(q)}`, { signal: current.signal });
return res.json();
}
This prevents race conditions where a slow response for "re" overwrites the result for "react".
Timeouts without manual timers:
const res = await fetch(url, { signal: AbortSignal.timeout(5000) });
// rejects with a DOMException named 'TimeoutError', not 'AbortError'
Combine user cancellation and timeout:
const signal = AbortSignal.any([controller.signal, AbortSignal.timeout(5000)]);
React effects:
useEffect(() => {
const controller = new AbortController();
fetch(`/api/items/${id}`, { signal: controller.signal })
.then((r) => r.json())
.then(setItem)
.catch((e) => { if (e.name !== 'AbortError') setError(e); });
return () => controller.abort();
}, [id]);
In Strict Mode development, the effect runs, cleans up and runs again, so you will see one aborted request in DevTools. That's expected.
One signal, many cleanups:
const ac = new AbortController();
window.addEventListener('resize', onResize, { signal: ac.signal });
document.addEventListener('keydown', onKey, { signal: ac.signal });
ac.abort(); // removes both listeners
function delay(ms, { signal } = {}) {
return new Promise((resolve, reject) => {
signal?.throwIfAborted();
const id = setTimeout(resolve, ms);
signal?.addEventListener('abort', () => {
clearTimeout(id);
reject(signal.reason);
}, { once: true });
});
}
Use signal.aborted, signal.reason and signal.throwIfAborted() inside long-running loops too.
POST. Cancellation is a client concern; make mutations idempotent.err.name (AbortError, TimeoutError) or compare err === signal.reason.abort() after the response has fully been read does nothing.AbortSignal.timeout and AbortSignal.any (widely available since 2023–2024) remove most hand-written timeout code.addEventListener, many Node APIs and libraries like TanStack Query, which passes a signal to your query function.Spotted a mistake or a rights concern? Report an issue