promise-cancellation

1.0.1 • Public • Published

promise-cancellation

Build Status

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();
}
 

Readme

Keywords

none

Package Sidebar

Install

npm i promise-cancellation

Weekly Downloads

0

Version

1.0.1

License

MIT

Last publish

Collaborators

  • futpib