AbortController
In one line
AbortController provides a signal you pass to async operations (like fetch) so you can cancel them later.
In simple words
AbortController is a small object that produces an AbortSignal. You pass that signal to an async operation — most commonly fetch — and later call controller.abort() to cancel it. The operation rejects with an AbortError, freeing resources and stopping network traffic.
This solves the stale request problem: a user types in a search box, you fire a fetch, they type again — you want to cancel the first request so its (now-outdated) response does not overwrite the second. Without abort, both requests complete and the last one to resolve wins, causing flicker and wasted bandwidth.
AbortSignal can be passed to fetch, addEventListener, ReadableStream, and increasingly other APIs. You can also combine multiple signals with AbortSignal.any, or use AbortSignal.timeout(ms) for a self-canceling signal. In React, abort in useEffect cleanup to avoid setState after unmount.
Example
let currentController = null;
async function search(query) {
currentController?.abort(); // cancel previous
currentController = new AbortController();
try {
const res = await fetch(`/api/search?q=${query}`, { signal: currentController.signal });
return await res.json();
} catch (e) {
if (e.name === "AbortError") return null; // expected cancel
throw e;
}
}
search("a"); search("ab"); // first request is abortedEach search aborts the previous controller. The aborted fetch rejects with AbortError, which we treat as expected and ignore.
Related terms
Related Resources
Related Lessons
- Fetch API
Learn request cancellation.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.