AbortController and AbortSignal

Abort signals, abort reasons, AbortSignal.abort, and AbortSignal.any, verified in Node.js 22.23.3, without cancelling a network request.

モジュールページの本文は現在英語です。

AbortController exposes an AbortSignal that other APIs can observe. abort(reason) marks the signal aborted and, if a reason is passed, stores it on signal.reason. This is only a signal. It does not, by itself, cancel work on a server, close a socket, or undo a completed request. A caller still has to pass the signal into an API that honors it, such as fetch, and even then the server may continue.

Examples were checked in Node.js 22.23.3 and never call fetch. This is not a browser test of cancelled network activity.

Examples were checked in Node.js 22.23.3, which provides these Web APIs as built-in globals. This is not a browser DOM test, and the examples do not send network requests. See the MDN documentation for the complete API, including browser support.

Web APIs overview → · JavaScript tutorial and built-in APIs →

API index

APISummary
AbortController.abort(), signal.throwIfAborted(), AbortSignal.abort()Abort a signal with a reason, throw if it is already aborted, or create an already aborted signal.
AbortSignal.any()Create a signal that aborts when any of the input signals abort, without touching the other sources.

Abort, reason, and throwIfAborted

A fresh signal has aborted false and reason undefined. abort("stop") sets aborted to true and reason to the string "stop". throwIfAborted() then throws that same string in this runtime. AbortSignal.abort("done") returns an already aborted signal with that reason, without an associated controller.

The abort event runs once. A second abort("second") does not increment the listener count, and the reason stays "first". abort() with no argument uses a DOMException whose name is AbortError; throwIfAborted() throws that exception. Node.js 22.23.3 reported the default message "This operation was aborted"; the example prints only the name.

AbortController.abort(), signal.throwIfAborted(), AbortSignal.abort()

new AbortController()
AbortController.prototype.abort(reason)
AbortSignal.prototype.throwIfAborted()
AbortSignal.abort(reason)

Abort a signal with a reason, throw if it is already aborted, or create an already aborted signal.

ParameterDescription
reasonAn optional value stored on signal.reason and thrown by throwIfAborted. If omitted, Node.js 22.23.3 uses a DOMException named AbortError.

Returns: abort returns undefined. AbortSignal.abort returns an aborted AbortSignal.

Throws

  • signal.reason — throwIfAborted throws the stored reason. After abort("stop") this was the string "stop". After abort() with no argument it was a DOMException named AbortError.
  • Listeners are notified when the signal first becomes aborted. A later abort call does not fire them again in this measurement.
javascript
const controller = new AbortController();
console.log(controller.signal.aborted);
console.log(controller.signal.reason);
controller.abort("stop");
console.log(controller.signal.aborted);
console.log(controller.signal.reason);
try {
  controller.signal.throwIfAborted();
} catch (error) {
  console.log(typeof error, error);
}

const already = AbortSignal.abort("done");
console.log(already.aborted, already.reason);

const once = new AbortController();
let count = 0;
once.signal.addEventListener("abort", () => {
  count += 1;
});
once.abort("first");
once.abort("second");
console.log(count, once.signal.reason);

const fallback = new AbortController();
fallback.abort();
try {
  fallback.signal.throwIfAborted();
} catch (error) {
  console.log(error.name);
}

Expected output

false
undefined
true
stop
string stop
true done
1 first
AbortError

Related: Request and Response

MDN documentation →

Combining signals, and what abort does not do

AbortSignal.any([first, second]) becomes aborted when either source is aborted, and it stores that source's reason. In the example, aborting the second controller leaves the first controller's signal unabort. AbortSignal.any was available in Node.js 22.23.3.

This still does not cancel a server operation. The example never calls fetch, so no HTTP request is started or cancelled. Pass the signal to an API that documents abort support if you want it to stop its own work; do not assume the remote side stopped.

AbortSignal.any()

AbortSignal.any(iterable)

Create a signal that aborts when any of the input signals abort, without touching the other sources.

ParameterDescription
iterableAn iterable of AbortSignal objects.

Returns: A new AbortSignal that becomes aborted with the reason of the first source that aborts.

  • AbortSignal.timeout exists in this Node.js runtime. It is not demonstrated here because a waiting timeout would make the example time-dependent.
javascript
const first = new AbortController();
const second = new AbortController();
const combined = AbortSignal.any([first.signal, second.signal]);
console.log(combined.aborted);
second.abort("from-second");
console.log(combined.aborted);
console.log(combined.reason);
console.log(first.signal.aborted);

Expected output

false
true
from-second
false

MDN documentation →