christian 4571a14169 android fix | před 3 roky | |
---|---|---|
.. | ||
index.d.ts | před 3 roky | |
index.js | před 3 roky | |
license | před 3 roky | |
package.json | před 3 roky | |
readme.md | před 3 roky |
Create a promise that can be canceled
Useful for animation, loading resources, long-running async computations, async iteration, etc.
$ npm install p-cancelable
const PCancelable = require('p-cancelable');
const cancelablePromise = new PCancelable((resolve, reject, onCancel) => {
const worker = new SomeLongRunningOperation();
onCancel(() => {
worker.close();
});
worker.on('finish', resolve);
worker.on('error', reject);
});
(async () => {
try {
console.log('Operation finished successfully:', await cancelablePromise);
} catch (error) {
if (cancelablePromise.isCanceled) {
// Handle the cancelation here
console.log('Operation was canceled');
return;
}
throw error;
}
})();
// Cancel the operation after 10 seconds
setTimeout(() => {
cancelablePromise.cancel('Unicorn has changed its color');
}, 10000);
Same as the Promise
constructor, but with an appended onCancel
parameter in executor
.
Cancelling will reject the promise with PCancelable.CancelError
. To avoid that, set onCancel.shouldReject
to false
.
const PCancelable = require('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
PCancelable
is a subclass of Promise
.
Type: Function
Accepts a function that is called when the promise is canceled.
You’re not required to call this function. You can call this function multiple times to add multiple cancel handlers.
Type: Function
Cancel the promise and optionally provide a reason.
The cancellation is synchronous. Calling it after the promise has settled or multiple times does nothing.
Type: boolean
Whether the promise is canceled.
Type: Error
Rejection reason when .cancel()
is called.
It includes a .isCanceled
property for convenience.
Convenience method to make your promise-returning or async function cancelable.
The function you specify will have onCancel
appended to its parameters.
const PCancelable = require('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();
In American English, the verb cancel is usually inflected canceled and canceling—with one l.
Both a browser API and the Cancelable Promises proposal use this spelling.
It’s still an early draft and I don’t really like its current direction. It complicates everything and will require deep changes in the ecosystem to adapt to it. And the way you have to use cancel tokens is verbose and convoluted. I much prefer the more pragmatic and less invasive approach in this module. The proposal was withdrawn.
.then()
or .catch()
is calledMIT © Sindre Sorhus