class AbortSignal extends EventTarget
The AbortSignal is used to notify observers when the
abortController.abort() method is called.
AbortSignal.abort
History
AbortSignal.abort(reason?): AbortSignal
anyAbortSignalReturns a new already aborted AbortSignal.
AbortSignal.timeout(delay): void
numberReturns a new AbortSignal which will be aborted in delay milliseconds.
AbortSignal.any(signals): void
AbortSignal[]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.
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.
booleanTrue after the AbortController has been aborted.
FunctionAn optional callback function that may be set by user code to be notified
when the abortController.abort() function has been called.
anyAn optional reason specified when the AbortSignal was triggered.
const ac = new AbortController(); ac.abort(new Error('boom!')); console.log(ac.signal.reason); // Error: boom!
abortSignal.throwIfAborted(): void
If abortSignal.aborted is true, throws abortSignal.reason.