1.0.0 • Published 1 year ago

@blakgeek/cancellable-promise v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Cancellable Promise

Basic Usage

const cancellablePromise = new CancellablePromise<LongRunningResult>((resolve, reject) => {
    
    resolve(runLongRunningProcess())
}, (reason: string) => {
    console.warn('request cancelled')
});

// cancel the promise if it runs longer than 10s
setTimeout(() => {
    cancellablePromise.cancel();
}, 10000)
console.log(await cancellablePromise);

Make a native Promise cancellable

const cancellablePromise = CancellablePromise.fromPromise(runLongRunningProcess());

// cancel the promise if it runs longer than 10s
setTimeout(() => {
    cancellablePromise.cancel();
}, 10000)
console.log(await cancellablePromise);