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().
const signal = AbortSignal.timeout(5000);
Try it
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.
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
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
TimeoutErrorversusAbortError: a signal fromAbortSignal.timeout()aborts with aDOMExceptionnamedTimeoutError(code23). A manualabort()givesAbortError(code20). Checksignal.reason.nameto 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 itsreasonfrom whichever input aborts first, and it is already aborted if any input is.- Events fire once. The
abortevent does not bubble, and listeners added after abortion never run: checksignal.abortedfirst. - 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 forevents.once, streams andfs. - Listener limits: adding many abort listeners to one signal can print a
MaxListenersExceededWarning; useevents.setMaxListeners(n, signal)if that is intended. AbortSignal.timeout()was added in Node.js 17.3 andany()in 20.3.