Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

AbortController — cancelling fetch operations

AbortController communicates a cancellation request through `signal`. `fetch` constructs a `Request` before checking the signal. If request construction fails, the promise rejects with that error (for example, a `TypeError` for an invalid URL), even if the signal is already aborted. If `fetch` detects the abort while its promise is pending, it rejects the promise with the abort reason. If the promise has already fulfilled with a `Response`, aborting cannot change its state but can cause a pending response-body read to fail.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

const controller = new AbortController();
const signal = controller.signal;
controller.abort(reason);
fetch(input, { signal });

AbortController is a Web API, not part of a particular ECMAScript edition. AbortController.signal denotes the instance property controller.signal; AbortController.abort denotes the instance method controller.abort(reason), not a static method.

Pass signal to fetch. abort(reason) signals cancellation; the default reason is a DOMException named AbortError. A signal cannot be reset: a new operation requiring independent cancellation needs a new controller.

If fetch successfully constructs a Request and finds that the signal is already aborted, it rejects the promise with the abort reason. Cancellation can also interrupt response-body consumption, but does not undo server work. Handle rejection rather than ignoring errors.

The example aborts before fetch, removing timing races. Check availability in the target browser; Node 24 in the repository does not establish browser support.

Examples

(async () => {
  const controller = new AbortController();
  const reason = new Error("Cancelled");
  controller.abort(reason);

  try {
    await fetch("data:text/plain,hello", {
      signal: controller.signal
    });
    console.log("unexpected success");
  } catch (error) {
    console.log(controller.signal.aborted);
    console.log(error === reason);
  }
})();

Result

true
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources