@daniele.fedeli/cancellable-promise
v1.0.1
Published
An easy way to cancel promises in Javascript
Downloads
6
Maintainers
Readme
Cancellable
Easy way to cancel async work in javascript
How to install
npm i @daniele.fedeli/cancellable-promise
How it works
The usage is pretty strightforward:
const Cancellable = require('@daniele.fedeli/cancellable-promise');
// const { Cancellable } = require('@daniele.fedeli/cancellable-promise');
const aLongRunningPromise = //
const { promise, abortController } = new Cancellable(aLongRunningPromise);
promise
.then(console.log)
.catch(console.error);
if(someCondition) {
abortController.abort();
}
Is it possibile also to pass an abort controller along with the promise as parameters.
const Cancellable = require('@daniele.fedeli/cancellable-promise');
// const { Cancellable } = require('@daniele.fedeli/cancellable-promise');
const abortController = new AbortController();
const aLongRunningPromise = //
const args = {promise: aLongRunningPromise, abortController}
// If an abortController is passed, then only the promise is returned
const promise = new Cancellable(aLongRunningPromise);
promise
.then(console.log)
.catch(console.error);
if(someCondition) {
abortController.abort();
}