@blakgeek/cancellable-promise
v1.0.0
Published
A functional cancellable promise implementation.
Downloads
244
Readme
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);