@krivega/cancelable-promise
v1.1.3
Published
Cancelable abstraction over promise
Downloads
1,390
Readme
cancelable-promise
Various abstractions over promises
Install
npm
npm install @krivega/cancelable-promise
yarn
yarn add @krivega/cancelable-promise
Usage
cancelablePromise
import cancelablePromise, {
isCanceledError
} from '@krivega/cancelable-promise/dist/cancelablePromise';
const basePromise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('done');
}, 5000);
});
const promise = cancelablePromise(basePromise);
promise.cancel();
promise
.then(() => {
// will not be called
})
.catch(error => {
if (isCanceledError(error)) {
console.log('promise is canceled!');
}
});
CancelableRequest
import CancelableRequest, {
isCanceledError
} from '@krivega/cancelable-promise/dist/CancelableRequest';
const request = () =>
new Promise((resolve, reject) => {
setTimeout(() => {
resolve('done');
}, 5000);
});
const cancelableRequester = new CancelableRequest(request);
const promise1 = cancelableRequest.request();
const promise2 = cancelableRequest.request();
promise1
.then(() => {
// will not be called
})
.catch(error => {
if (isCanceledError(error)) {
console.log('promise is canceled!');
}
});
promise1.then(() => {
console.log('done');
});
API
CancelableRequest.cancelRequest
const cancelableRequester = new CancelableRequest(request);
const promise = cancelableRequest.request();
cancelableRequest.cancelRequest();
promise
.then(() => {
// will not be called
})
.catch(error => {
if (isCanceledError(error)) {
console.log('promise is canceled!');
}
});
Run tests
npm test
Maintainer
Krivega Dmitriy
- Website: https://krivega.com
- Github: @Krivega
Contributing
Contributions, issues and feature requests are welcome!Feel free to check issues page. You can also take a look at the contributing guide.
📝 License
Copyright © 2020 Krivega Dmitriy. This project is MIT licensed.