Optional
value: ValueType | PromiseLike<ValueType>Optional
reason: unknownimport PCancelable from 'p-cancelable';
const cancelablePromise = new PCancelable((resolve, reject, onCancel) => {
const job = new Job();
onCancel.shouldReject = false;
onCancel(() => {
job.stop();
});
job.on('finish', resolve);
});
cancelablePromise.cancel(); // Doesn't throw an error
Readonly
[toCancel the promise and optionally provide a reason.
The cancellation is synchronous. Calling it after the promise has settled or multiple times does nothing.
Optional
reason: stringThe cancellation reason to reject the promise with.
Readonly
isWhether the promise is canceled.
Static
Readonly
[species]Attaches a callback for only the rejection of the Promise.
Optional
onrejected: null | ((reason) => TResult | PromiseLike<TResult>)The callback to execute when the Promise is rejected.
A Promise for the completion of the callback.
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.
Attaches callbacks for the resolution and/or rejection of the Promise.
Optional
onfulfilled: null | ((value) => TResult1 | PromiseLike<TResult1>)The callback to execute when the Promise is resolved.
Optional
onrejected: null | ((reason) => TResult2 | PromiseLike<TResult2>)The callback to execute when the Promise is rejected.
A Promise for the completion of which ever callback is executed.
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 iterable of Promises.
A new Promise.
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.
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.
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.
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.
An array or iterable of Promises.
A new Promise.
Static
fnConvenience method to make your promise-returning or async function cancelable.
Convenience method to make your promise-returning or async function cancelable.
import PCancelable from 'p-cancelable';
const fn = PCancelable.fn((input, onCancel) => {
const job = new Job();
onCancel(() => {
job.cleanup();
});
return job.start(); //=> Promise
});
const cancelablePromise = fn('input'); //=> PCancelable
// …
cancelablePromise.cancel();
import PCancelable from 'p-cancelable';
const fn = PCancelable.fn((input, onCancel) => {
const job = new Job();
onCancel(() => {
job.cleanup();
});
return job.start(); //=> Promise
});
const cancelablePromise = fn('input'); //=> PCancelable
// …
cancelablePromise.cancel();
Rest
...arguments: unknown[]Rest
...arguments: unknown[]Static
raceCreates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.
An iterable of Promises.
A new Promise.
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.
Static
rejectStatic
resolveCreates a new resolved promise.
A resolved promise.
Creates a new resolved promise for the provided value.
A promise.
A promise whose internal state matches the provided promise.
Creates a new resolved promise for the provided value.
A promise.
A promise whose internal state matches the provided promise.
Create a promise that can be canceled.
Can be constructed in the same was as a
Promise
constructor, but with an appendedonCancel
parameter inexecutor
.PCancelable
is a subclass ofPromise
.Cancelling will reject the promise with
CancelError
. To avoid that, setonCancel.shouldReject
tofalse
.