Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/krivega/cancelable-promise
https://github.com/krivega/cancelable-promise
Last synced: 8 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/krivega/cancelable-promise
- Owner: Krivega
- License: mit
- Created: 2020-02-23T14:28:13.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-05-21T11:53:52.000Z (7 months ago)
- Last Synced: 2024-12-12T22:38:43.172Z (11 days ago)
- Language: TypeScript
- Size: 552 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# cancelable-promise
[![npm](https://img.shields.io/npm/v/@krivega/cancelable-promise?style=flat-square)](https://www.npmjs.com/package/@krivega/cancelable-promise)
![npm bundle size](https://img.shields.io/bundlephobia/minzip/@krivega/cancelable-promise?style=flat-square)Various abstractions over promises
## Install
npm
```sh
npm install @krivega/cancelable-promise
```yarn
```sh
yarn add @krivega/cancelable-promise
```## Usage
### cancelablePromise
```js
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
```js
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
```js
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
```sh
npm test
```## Maintainer
**Krivega Dmitriy**
- Website: https://krivega.com
- Github: [@Krivega](https://github.com/Krivega)## Contributing
Contributions, issues and feature requests are welcome!
Feel free to check [issues page](https://github.com/Krivega/cancelable-promise/issues). You can also take a look at the [contributing guide](https://github.com/Krivega/cancelable-promise/blob/master/CONTRIBUTING.md).## 📝 License
Copyright © 2020 [Krivega Dmitriy](https://github.com/Krivega).
This project is [MIT](https://github.com/Krivega/cancelable-promise/blob/master/LICENSE) licensed.