Skip to content

A replacement for Typescript's default awaiter helper that supports Bluebird promise cancellations.

Notifications You must be signed in to change notification settings

itaysabato/cancelable-awaiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cancelable Awaiter

A replacement for Typescript's default awaiter helper that supports Bluebird promise cancellations.

Installation

npm install --save cancelable-awaiter

Usage

Depends on bluebird being installed as a dependency and configured to support cancellations:

import * as tslib from "tslib";
import * as Bluebird from "bluebird";
import * as awaiter from "cancelable-awaiter";

Bluebird.config({
    cancellation: true
});

(tslib as any).__awaiter = awaiter;

Note that in order for the above to work you also need to compile your project with the --importHelpers flag and install the tslib module.

Then async/await syntax can be used in conjunction with promise cancellations:

async function doSomeThings() {
    await doFirstThing()
        // will be invoked anyway:
        .finally(() => console.log("May have done first thing."));

    // may not execute this part if cancelled beforehand:
    console.log("Done first thing!");

    // may also be cancelled after first thing is done:
    await doSecondThing();
    console.log("Done second thing!");
}

const promise = doSomeThings()
    .finally(() => console.log("May have done some things."));

Promise.delay(1000)
    .then(() => promise.cancel());

About

A replacement for Typescript's default awaiter helper that supports Bluebird promise cancellations.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published