1.0.0 • Published 8 years ago
@ybq/p-cancelable v1.0.0
CancelablePromise
Promise that can be canceled, learned from sindresorhus/p-cancelable.
The difference from sindresorhus version is that CancelablePromise in this lib is not a subclass of Promise.
Install
$ npm i @ybq/p-cancelableUsage
const PCancelable = require('@ybq/p-cancelable');
const cancelablePromise = new PCancelable((onCancel, resolve, reject) => {
    const worker = new SomeLongRunningOperation();
    onCancel(() => {
        worker.close();
    });
    worker.on('finish', resolve);
    worker.on('error', reject);
});
cancelablePromise
    .then(value => {
        console.log('Operation finished successfully:', value);
    })
    .catch(reason => {
        if (cancelablePromise.canceled) {
            // Handle the cancelation here
            console.log('Operation was canceled');
            return;
        }
        throw reason;
    });
// Cancel the operation after 10 seconds
setTimeout(() => {
    cancelablePromise.cancel();
}, 10000);API
new PCancelable(executor)
Same as the Promise constructor, but with a prepended onCancel parameter in executor.
PCancelable is a subclass of Promise.
onCanceled(fn)
Type: Function
Accepts a function that is called when the promise is canceled.
You're not required to call this function.
PCancelable#cancel()
Type: Function
Cancel the promise. The cancellation is synchronous.
Calling it after the promise has settled or multiple times does nothing.
PCancelable#canceled
Type: boolean
Whether the promise is canceled.
1.0.0
8 years ago