Cancel a Fetch request: AbortController and result handling
A browser Fetch request can be canceled by passing an AbortController signal and calling controller.abort(). The fetch promise rejects with AbortError. Cancellation can also interrupt response-body reading after response headers arrive. It stops the client-side wait and body processing; it does not guarantee that work already started by the server is canceled or rolled back.
A browser Fetch request can be canceled by passing an AbortController signal and calling controller.abort(). The fetch promise rejects with AbortError. Cancellation can also interrupt response-body reading after response headers arrive. It stops the client-side wait and body processing; it does not guarantee that work already started by the server is canceled or rolled back.
Cancel from a user action
Create a new controller for each operation:
const controller = new AbortController();
async function loadFile() {
try {
const response = await fetch("/files/large.bin", {
signal: controller.signal
});
if (!response.ok) throw new Error("HTTP " + response.status);
const file = await response.blob();
showFile(file);
} catch (error) {
if (error.name === "AbortError") {
showMessage("Request canceled.");
return;
}
showError(error);
}
}
cancelButton.addEventListener("click", () => controller.abort());An AbortSignal is one-use: once aborted, a later fetch using it fails immediately. Sharing one controller among several requests means one abort affects them all, so do that only when group cancellation is intended.
Timeouts and multiple cancellation causes
AbortSignal.timeout(milliseconds) creates a timeout signal. A timeout rejects with TimeoutError, distinct from a user-triggered AbortError. Check runtime support and handle each reason appropriately. User cancellation is usually expected control flow, not an application error.
Server-side work is a separate question
The browser may stop waiting after the server has already received the request and started a database write, payment, or email. A missing response does not prove the operation failed. If cancellation must stop server work, define cancellation behavior in the API and propagate it through server-side child tasks. Before retrying a write, use the API’s recovery mechanism, such as an idempotency key or job-status lookup.
Fetch can fulfill with a Response as soon as status and headers arrive, before the body is fully read. Aborting then can make body consumption reject with AbortError. Treat header receipt and body reading as separate stages and clean up the response stream as needed.
Checklist
Create a fresh controller per operation.
Pass its signal to fetch.
Distinguish AbortError from TimeoutError.
Do not assume client cancellation rolls back server work.
Apply the API’s idempotency rules before retrying writes.
Example
const controller = new AbortController();
const response = await fetch("/files/large.bin", { signal: controller.signal });
const file = await response.blob();
// Call controller.abort() from the cancel action.Sources
[MDN Fetch API: Canceling a request](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#canceling_a_request)
[MDN AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
[WHATWG Fetch Standard](https://fetch.spec.whatwg.org/)