On this page

C

AbortSignal

History
class AbortSignal extends EventTarget

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

S

AbortSignal.abort

History
AbortSignal.abort(reason?): AbortSignal
Attributes
reason:any
Returns:AbortSignal

Returns a new already aborted AbortSignal.

S

AbortSignal.timeout

History
AbortSignal.timeout(delay): void
Attributes
delay:number
The number of milliseconds to wait before triggering the AbortSignal.

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

S

AbortSignal.any

History
AbortSignal.any(signals): void
Attributes
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.

E

abort

History

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();

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.

P

abortSignal.aborted

History
Type:boolean

True after the AbortController has been aborted.

P

abortSignal.onabort

History

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

P

abortSignal.reason

History
Type:any

An optional reason specified when the AbortSignal was triggered.

const ac = new AbortController();
ac.abort(new Error('boom!'));
console.log(ac.signal.reason);  // Error: boom!
M

abortSignal.throwIfAborted

History
abortSignal.throwIfAborted(): void

If abortSignal.aborted is true, throws abortSignal.reason.