Optional
signal: AbortSignalReadonly
[toStatic
Readonly
[species]Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The resolved value cannot be modified from the callback.
Optional
onfinally: null | (() => void)The callback to execute when the Promise is settled (fulfilled or rejected).
A Promise for the completion of the callback.
Static
allCreates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.
An array of Promises.
A new Promise.
Static
allCreates a Promise that is resolved with an array of results when all of the provided Promises resolve or reject.
An array of Promises.
A new Promise.
Static
anyThe any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.
An array or iterable of Promises.
A new Promise.
The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.
A new Promise.
Static
raceStatic
rejectCreates a new rejected promise for the provided reason.
Optional
reason: anyThe reason the promise was rejected.
A new rejected Promise.
Static
resolveCreates a new resolved promise.
A resolved promise.
Extends Promise constructor to accept not just a
(resolve, reject) => void
function, but(resolve, reject) => cleanupOnAbortFn
andAbortSignal
Helps to write promises that can be cancelled without leaking
abort
listeners on the signal, and without forgetting to write cleanup logic. See the example belowSemantics:
Adds
abort
listener onAbortSignal
. After the promise settles, the listener is always removed (no leaks)If
abort
fires beforeresolve
orreject
is called, callscleanupOnAbort
function is called and rejects withAbortedError
If
resolve
orreject
is called beforeabort
is fired, resolves/rejects as a regular promisecleanupOnAbort
is guaranteed to be called at most once, and only ifresolve
orreject
has not been called yetRace condition is possible where
abort
fires, thanresolve
orreject
is performed beforeabort
is handled. In such caseresolve
/reject
always wins.If passed
AbortSignal
is already aborted, does not callexecutor
at all, and rejects withAbortedError
Example
Promisified
setTimeout
with cancellation that removes the timer