Global objects#

Stability: 2 - Stable

These objects are available in all modules.

The following variables may appear to be global but are not. They exist only in the scope of CommonJS modules:

The objects listed here are specific to Node.js. There are built-in objects that are part of the JavaScript language itself, which are also globally accessible.

__dirname#

This variable may appear to be global but is not. See __dirname.

__filename#

This variable may appear to be global but is not. See __filename.

Class: AbortController#

A utility class used to signal cancelation in selected Promise-based APIs. The API is based on the Web API <AbortController>.

const ac = new AbortController();

ac.signal.addEventListener('abort', () => console.log('Aborted!'),
                           { once: true });

ac.abort();

console.log(ac.signal.aborted);  // Prints true
js

abortController.abort([reason])#

  • reason <any> An optional reason, retrievable on the AbortSignal's reason property.

Triggers the abort signal, causing the abortController.signal to emit the 'abort' event.

abortController.signal#

Class: AbortSignal#

The AbortSignal is used to notify observers when the abortController.abort() method is called.

Static method: AbortSignal.abort([reason])#

Returns a new already aborted AbortSignal.

Static method: AbortSignal.timeout(delay)#

  • delay <number> The number of milliseconds to wait before triggering the AbortSignal.

Returns a new AbortSignal which will be aborted in delay milliseconds.

Static method: AbortSignal.any(signals)#

  • signals <AbortSignal>[] The AbortSignals of which to compose a new AbortSignal.

Returns a new AbortSignal which will be aborted if any of the provided signals are aborted. Its abortSignal.reason will be set to whichever one of the signals caused it to be aborted.

Event: 'abort'#

The 'abort' event is emitted when the abortController.abort() method is called. The callback is invoked with a single object argument with a single type property set to 'abort':

const ac = new AbortController();

// Use either the onabort property...
ac.signal.onabort = () => console.log('aborted!');

// Or the EventTarget API...
ac.signal.addEventListener('abort', (event) => {
  console.log(event.type);  // Prints 'abort'
}, { once: true });

ac.abort();
js

The AbortController with which the AbortSignal is associated will only ever trigger the 'abort' event once. We recommended that code check that the abortSignal.aborted attribute is false before adding an 'abort' event listener.

Any event listeners attached to the AbortSignal should use the { once: true } option (or, if using the EventEmitter APIs to attach a listener, use the once() method) to ensure that the event listener is removed as soon as the 'abort' event is handled. Failure to do so may result in memory leaks.

abortSignal.aborted#

True after the AbortController has been aborted.

abortSignal.onabort#

An optional callback function that may be set by user code to be notified when the abortController.abort() function has been called.

abortSignal.reason#