Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Globals and Console

The AbortSignal class

Reference for AbortSignal in Node.js: aborted, reason, throwIfAborted(), AbortSignal.abort(), AbortSignal.timeout() and AbortSignal.any() with examples.

An AbortSignal is the read side of cancellation. It reports whether an operation has been told to stop (aborted), why (reason), and fires an abort event when that happens. You normally get one from controller.signal, but the class also has static helpers that create ready-made signals: AbortSignal.abort(), AbortSignal.timeout() and AbortSignal.any().

A signal is an EventTarget, so the usual addEventListener and removeEventListener work on it. It cannot be constructed directly with new AbortSignal().

NODE
const signal = AbortSignal.timeout(5000);

Try it

Node.js timeout, abort and reasons

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Combining signals with any()

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
signal.aborted        // boolean
signal.reason         // any
signal.onabort        // event handler
signal.throwIfAborted(): void

AbortSignal.abort(reason?): AbortSignal
AbortSignal.timeout(milliseconds): AbortSignal
AbortSignal.any(signals: AbortSignal[]): AbortSignal
Name Description
aborted true once the signal has been aborted.
reason The value passed to abort(), or a DOMException (AbortError by default, TimeoutError for timeout()). undefined until aborted.
throwIfAborted() Throws reason if the signal is aborted; otherwise does nothing.
milliseconds Delay for timeout(). Must be a non-negative integer-like number.
signals Array of signals for any().

Returns: the static methods return a new AbortSignal.

Errors: timeout() throws ERR_OUT_OF_RANGE for an invalid delay; any() throws ERR_INVALID_ARG_TYPE if an element is not a signal.

Usage notes

  • TimeoutError versus AbortError: a signal from AbortSignal.timeout() aborts with a DOMException named TimeoutError (code 23). A manual abort() gives AbortError (code 20). Check signal.reason.name to tell a deadline from a user cancel.
  • The timeout timer is unref'd. An AbortSignal.timeout() signal does not keep the process alive by itself, and it only fires if something else is running.
  • any() follows the first abort. The result takes its reason from whichever input aborts first, and it is already aborted if any input is.
  • Events fire once. The abort event does not bubble, and listeners added after abortion never run: check signal.aborted first.
  • Cleanup: with { once: true } the listener removes itself after firing. If the work succeeds first, remove it manually.
  • Passing signals around: almost every promise-based Node API takes { signal }. See the lesson for events.once, streams and fs.
  • Listener limits: adding many abort listeners to one signal can print a MaxListenersExceededWarning; use events.setMaxListeners(n, signal) if that is intended.
  • AbortSignal.timeout() was added in Node.js 17.3 and any() in 20.3.