Glossary / AbortController

AbortController

AbortController is a controller object whose abort() method signals cancellation through its associated AbortSignal. APIs that support the signal can stop work and reject/close appropriately. It is a common web platform mechanism for fetch requests, streams, event listeners, and other abortable operations, with broad support in modern browsers and Node.js.

Basic request and lifecycle

const controller = new AbortController();
const responsePromise = fetch(url, { signal: controller.signal });
controller.abort(new Error("View changed"));

A signal is one-shot. Once aborted, operations using it should treat it as already canceled. Create a new controller for a new operation/lifecycle. abort(reason) can carry a reason; APIs may surface AbortError or the supplied reason according to the operation. Combine signals with AbortSignal.any where supported, and use AbortSignal.timeout when supported, while preserving compatibility fallbacks.

Controllers are useful for component teardown, request replacement, and overall deadlines. Aborting fetch can stop transport/body consumption, but it cannot undo a server-side action already committed. Keep cancellation separate from user-visible application failure. If several tasks share a controller, aborting it cancels all listeners/operations attached to that signal; scope controllers according to ownership.

Interview questions

  1. How cancel a fetch? Pass controller.signal and call controller.abort().
  2. Can an aborted signal be reused? It remains aborted; create a new controller for a new operation.
  3. Does abort undo server side effects? No; it only signals local/API cancellation and may arrive after commit.
  4. How cancel a group of operations? Pass the same signal or compose signals where supported.

References