Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Promises and Async

The AbortController interface

Reference for AbortController and AbortSignal: cancelling fetch and custom async work, abort reasons, timeouts, combining signals, and cleanup, with examples.

An AbortController lets you cancel work that is already under way. You create a controller, hand its signal to the operation, and call controller.abort() when you want it to stop. The signal is an AbortSignal: an event target that reports whether cancellation was requested and tells listeners when it happens.

APIs such as fetch() and addEventListener() accept a signal directly. You can also write your own functions that observe one, as the first example does.

JavaScript
const controller = new AbortController();
fetch("/api/data", { signal: controller.signal });
controller.abort();

Try it

JS An abortable sleep

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Reasons, events and timeouts

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
const controller = new AbortController();
controller.signal           // the AbortSignal
controller.abort()          // abort with the default reason
controller.abort(reason)    // abort with any value as the reason

AbortSignal.abort(reason)           // an already-aborted signal
AbortSignal.timeout(milliseconds)   // aborts after a delay
AbortSignal.any([signalA, signalB]) // aborts when any input aborts
Member Description
signal.aborted true once the signal has been aborted.
signal.reason The abort reason: whatever you passed to abort(), or an AbortError DOMException by default. A timeout signal uses a TimeoutError.
signal.throwIfAborted() Throws the reason if the signal is aborted; otherwise does nothing.
abort event Fires once on the signal when it is aborted.

Return value of abort(): undefined.

Usage notes

  • A controller works once. After abort(), the signal stays aborted and later calls do nothing. Create a new controller for the next operation.
  • fetch() rejects with the signal's reason when aborted. Check error.name === "AbortError" to tell a cancellation from a real failure.
  • Event listeners: addEventListener("click", handler, { signal }) removes the listener when the signal aborts, a tidy way to unregister many listeners at once.
  • Check early. If your code can receive a signal that is already aborted, test signal.aborted (or call throwIfAborted()) before starting work.
  • Always remove your own abort listeners (use { once: true }) when the operation finishes first, so long-lived signals do not accumulate them.
  • Timeouts: AbortSignal.timeout(ms) is the simplest way to put a time limit on fetch(). Combine it with a user cancel button using AbortSignal.any().
  • Abort is cooperative. Aborting a signal cannot stop arbitrary running code; the operation must listen for it.
  • Reasons need not be errors. Passing a string works, but passing an Error or DOMException gives better stack traces.

Browser support

AbortController and AbortSignal are supported in all current browsers and in Node.js. AbortSignal.timeout(), AbortSignal.any() and throwIfAborted() are newer, so check a compatibility table if you support older browsers.