promise-cancellation
v1.0.1
Published
Nice cancellation for any Promise/A+ implementation
Downloads
7
Readme
promise-cancellation
Nice cancellation for any Promise/A+ implementation
- "Cancelled" promises are forever 'pending'
- Can be used with any Promise/A+
Install
As usual:
yarn add promise-cancellation
or
npm install promise-cancellation
Example
import {Cancellation} from 'promise-cancellation';
function main() {
const cancellation = new Cancellation();
doSomething(cancellation.token);
if (youChangedYourMind) {
cancellation.cancel();
}
}
function doSomething(cancellationToken) {
return doOneThing()
// will hang if cancelled, otherwise will act as identity
.then(cancellationToken)
.then(doOtherThing);
}
async function doSomethingAsyncAwait(cancellationToken) {
await doOneThing();
// as above, this will never resolve if cancelled
await cancellationToken;
await doOtherThing();
}
async function doSomethingAgain(cancellationToken) {
await doOneThing();
if (cancellationToken.isCancelled()) {
return;
}
await doOtherThing();
}