Creates a new CancellablePromise.
A callback used to initialize the promise. This callback is passed two arguments:
a resolve callback used to resolve the promise with a value
or the result of another promise (possibly cancellable),
and a reject callback used to reject the promise with a provided reason or error.
If the value provided to the resolve callback is a thenable and cancellable object
(it has a then and a cancel method),
cancellation requests will be forwarded to that object and the oncancelled will not be invoked anymore.
If any one of the two callbacks is called after the promise has been cancelled,
the provided values will be cancelled and resolved as usual,
but their results will be discarded.
However, if the resolution process ultimately ends up in a rejection
that is not due to cancellation, the rejection reason
will be wrapped in a CancelledRejectionError
and bubbled up as an unhandled rejection.
Optionaloncancelled: CancellablePromiseCancellerIt is the caller's responsibility to ensure that any operation
started by the executor is properly halted upon cancellation.
This optional callback can be used to that purpose.
It will be called synchronously with a cancellation cause
when cancellation is requested, after the promise has already rejected
with a CancelError, but before
any then/catch/finally callback runs.
If the callback returns a thenable, the promise returned from cancel
will only fulfill after the former has settled.
Unhandled exceptions or rejections from the callback will be wrapped
in a CancelledRejectionError and bubbled up as unhandled rejections.
If the resolve callback is called before cancellation with a cancellable promise,
cancellation requests on this promise will be diverted to that promise,
and the original oncancelled callback will be discarded.
Cancels immediately the execution of the operation associated with this promise. The promise rejects with a CancelError instance as reason, with the CancelError#cause property set to the given argument, if any.
Has no effect if called after the promise has already settled; repeated calls in particular are safe, but only the first one will set the cancellation cause.
The CancelError exception need not be handled explicitly on the promises that are being cancelled:
cancelling a promise with no attached rejection handler does not trigger an unhandled rejection event.
Therefore, the following idioms are all equally correct:
new CancellablePromise((resolve, reject) => { ... }).cancel();
new CancellablePromise((resolve, reject) => { ... }).then(...).cancel();
new CancellablePromise((resolve, reject) => { ... }).then(...).catch(...).cancel();
Whenever some cancelled promise in a chain rejects with a CancelError
with the same cancellation cause as itself, the error will be discarded silently.
However, the CancelError will still be delivered to all attached rejection handlers
added by then and related methods:
let cancellable = new CancellablePromise((resolve, reject) => { ... });
cancellable.then(() => { ... }).catch(console.log);
cancellable.cancel(); // A CancelError is printed to the console.
If the CancelError is not handled downstream by the time it reaches
a non-cancelled promise, it will trigger an unhandled rejection event,
just like normal rejections would:
let cancellable = new CancellablePromise((resolve, reject) => { ... });
let chained = cancellable.then(() => { ... }).then(() => { ... }); // No catch...
cancellable.cancel(); // Unhandled rejection event on chained!
Therefore, it is important to either cancel whole promise chains from their tail, as shown in the correct idioms above, or take care of handling errors everywhere.
Optionalcause: anyA cancellable promise that fulfills after the cancel callback (if any)
and all handlers attached up to the call to cancel have run.
If the cancel callback returns a thenable, the promise returned by cancel
will also wait for that thenable to settle.
This enables callers to wait for the cancelled operation to terminate
without being forced to handle potential errors at the call site.
cancellable.cancel().then(() => {
// Cleanup finished, it's safe to do something else.
}, (err) => {
// Unreachable: the promise returned from cancel will never reject.
});
Note that the returned promise will not handle implicitly any rejection that might have occurred already in the cancelled chain. It will just track whether registered handlers have been executed or not. Therefore, unhandled rejections will never be silently handled by calling cancel.
Binds promise cancellation to the abort event of the given AbortSignal. If the signal has already aborted, the promise will be cancelled immediately. When either condition is verified, the cancellation cause will be set to the signal's abort reason (see AbortSignal.reason).
Has no effect if called (or if the signal aborts) after the promise has already settled. Only the first signal to abort will set the cancellation cause.
For more details about the cancellation process,
see cancel and the CancellablePromise constructor.
This method enables awaiting cancellable promises without having
to store them for future cancellation, e.g.:
await longRunningOperation().cancelOn(signal);
instead of:
let promiseToBeCancelled = longRunningOperation();
await promiseToBeCancelled;
This promise, for method chaining.
Attaches a callback for only the rejection of the Promise.
The optional oncancelled argument will be invoked when the returned promise is cancelled,
with the same semantics as the oncancelled argument of the constructor.
When the parent promise rejects or is cancelled, the onrejected callback will run,
even after the returned promise has been cancelled:
in that case, should it reject or throw, the reason will be wrapped
in a CancelledRejectionError and bubbled up as an unhandled rejection.
It is equivalent to
cancellablePromise.then(undefined, onrejected, oncancelled);
and the same caveats apply.
Optionalonrejected: ((reason: any) => TResult | PromiseLike<TResult>) | nullOptionaloncancelled: CancellablePromiseCancellerA Promise for the completion of the callback. Cancellation requests on the returned promise will propagate up the chain to the parent promise, but not in the other direction.
The promise returned from cancel will fulfill only after all attached handlers up the entire promise chain have been run.
If onrejected returns a cancellable promise,
cancellation requests will be diverted to it,
and the specified oncancelled callback will be discarded.
See then for more details.
Attaches a callback that is invoked when the CancellablePromise is settled (fulfilled or rejected). The resolved value cannot be accessed or modified from the callback. The returned promise will settle in the same state as the original one after the provided callback has completed execution, unless the callback throws or returns a rejecting promise, in which case the returned promise will reject as well.
The optional oncancelled argument will be invoked when the returned promise is cancelled,
with the same semantics as the oncancelled argument of the constructor.
Once the parent promise settles, the onfinally callback will run,
even after the returned promise has been cancelled:
in that case, should it reject or throw, the reason will be wrapped
in a CancelledRejectionError and bubbled up as an unhandled rejection.
This method is implemented in terms of then and the same caveats apply. It is polyfilled, hence available in every OS/webview version.
Optionalonfinally: (() => void) | nullOptionaloncancelled: CancellablePromiseCancellerA Promise for the completion of the callback. Cancellation requests on the returned promise will propagate up the chain to the parent promise, but not in the other direction.
The promise returned from cancel will fulfill only after all attached handlers up the entire promise chain have been run.
If onfinally returns a cancellable promise,
cancellation requests will be diverted to it,
and the specified oncancelled callback will be discarded.
See then for more details.
Attaches callbacks for the resolution and/or rejection of the CancellablePromise.
The optional oncancelled argument will be invoked when the returned promise is cancelled,
with the same semantics as the oncancelled argument of the constructor.
When the parent promise rejects or is cancelled, the onrejected callback will run,
even after the returned promise has been cancelled:
in that case, should it reject or throw, the reason will be wrapped
in a CancelledRejectionError and bubbled up as an unhandled rejection.
Optionalonfulfilled: The callback to execute when the Promise is resolved.
Optionalonrejected: The callback to execute when the Promise is rejected.
Optionaloncancelled: CancellablePromiseCancellerA CancellablePromise for the completion of whichever callback is executed.
The returned promise is hooked up to propagate cancellation requests up the chain, but not down:
onrejected handler will be invoked with a CancelError
and the returned promise will resolve regularly with its result;onrejected handler will still be invoked with the parent's CancelError,
but its result will be discarded
and the returned promise will reject with a CancelError as well.The promise returned from cancel will fulfill only after all attached handlers up the entire promise chain have been run.
If either callback returns a cancellable promise,
cancellation requests will be diverted to it,
and the specified oncancelled callback will be discarded.
StatictryTakes a callback of any kind (returns or throws, synchronously or asynchronously) and wraps its result in a Promise.
A function that is called synchronously. It can do anything: either return a value, throw an error, or return a promise.
Additional arguments, that will be passed to the callback.
A Promise that is:
StaticallCreates a CancellablePromise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.
Every one of the provided objects that is a thenable and cancellable object will be cancelled when the returned promise is cancelled, with the same cause.
Creates 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.
StaticallCreates a CancellablePromise that is resolved with an array of results when all of the provided Promises resolve or reject.
Every one of the provided objects that is a thenable and cancellable object will be cancelled when the returned promise is cancelled, with the same cause.
Creates 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.
StaticanyThe 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.
Every one of the provided objects that is a thenable and cancellable object will be cancelled when the returned promise is cancelled, with the same cause.
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.
An array or iterable of Promises.
A new Promise.
StaticcancelCreates a new cancelled CancellablePromise for the provided cause.
Optionalcause: anyStaticraceCreates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.
Every one of the provided objects that is a thenable and cancellable object will be cancelled when the returned promise is cancelled, with the same cause.
Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.
An array of Promises.
A new Promise.
StaticrejectCreates a new rejected CancellablePromise for the provided reason.
Optionalreason: anyStaticresolveCreates a new resolved CancellablePromise.
Creates a new resolved CancellablePromise for the provided value.
Creates a new resolved CancellablePromise for the provided value.
StaticsleepCreates a new CancellablePromise that resolves after the specified timeout. The returned promise can be cancelled without consequences.
Creates a new CancellablePromise that resolves after the specified timeout, with the provided value. The returned promise can be cancelled without consequences.
StatictimeoutCreates a new CancellablePromise that cancels after the specified timeout, with the provided cause.
If the AbortSignal.timeout factory method is available,
it is used to base the timeout on active time rather than elapsed time.
Otherwise, timeout falls back to setTimeout.
Optionalcause: anyStaticwithCreates a new CancellablePromise and returns it in an object, along with its resolve and reject functions and a getter/setter for the cancellation callback.
This method is polyfilled, hence available in every OS/webview version.
A promise with an attached method for cancelling long-running operations (see CancellablePromise#cancel). Cancellation can optionally be bound to an AbortSignal for better composability (see CancellablePromise#cancelOn).
Cancelling a pending promise will result in an immediate rejection with an instance of CancelError as reason, but whoever started the promise will be responsible for actually aborting the underlying operation. To this purpose, the constructor and all chaining methods accept optional cancellation callbacks.
If a
CancellablePromisestill resolves after having been cancelled, the result will be discarded. If it rejects, the reason will be reported as an unhandled rejection, wrapped in a CancelledRejectionError instance. To facilitate the handling of cancellation requests, cancelledCancellablePromises will not report unhandledCancelErrors whosecausefield is the same as the one with which the current promise was cancelled.All usual promise methods are defined and return a
CancellablePromisewhose cancel method will cancel the parent operation as well, propagating the cancellation reason upwards through promise chains. Conversely, cancelling a promise will not automatically cancel dependent promises downstream:Cancelling a promise that has already settled is safe and has no consequence.
The
cancelmethod returns a promise that always fulfills after the whole chain has processed the cancel request and all attached callbacks up to that moment have run.All ES2024 promise methods (static and instance) are defined on CancellablePromise, but actual availability may vary with OS/webview version.
In line with the proposal at https://github.com/tc39/proposal-rm-builtin-subclassing,
CancellablePromisedoes not support transparent subclassing. Extenders should take care to provide their own method implementations. This might be reconsidered in case the proposal is retired.CancellablePromise is a wrapper around the DOM Promise object and is compliant with the Promises/A+ specification (it passes the compliance suite) if so is the underlying implementation.