Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kevinokerlund/cancellable-promise
An extension of native ES6 Promises that provides the ability to cancel and/or clear promise chains.
https://github.com/kevinokerlund/cancellable-promise
clear-promise-chains es6-promise javascript-promise native-es6-promises promise promise-cancelling promise-chain promises
Last synced: 13 days ago
JSON representation
An extension of native ES6 Promises that provides the ability to cancel and/or clear promise chains.
- Host: GitHub
- URL: https://github.com/kevinokerlund/cancellable-promise
- Owner: kevinokerlund
- License: mit
- Created: 2017-04-08T17:01:19.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-04-09T04:57:42.000Z (almost 8 years ago)
- Last Synced: 2024-11-22T06:22:37.340Z (2 months ago)
- Topics: clear-promise-chains, es6-promise, javascript-promise, native-es6-promises, promise, promise-cancelling, promise-chain, promises
- Language: JavaScript
- Homepage:
- Size: 2.93 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cancellable-promise
An extension of native ES6 Promises that provides the ability to cancel and clear promise chains.## Usage
### Creating a new cancellable promise
Cancellable promises are created just like regular promises, the only difference is the constructor you use.
```javascript
let promise = new CancellablePromise((resolve, reject) => {
setTimeout(() => {
resolve('Resolved!');
}, 500)
});promise
.then(val => console.log(val))
.catch(val => console.log(val));
```### Cancelling a promise
Cancelling a promise, internally calls the "reject" on the promise.
```javascript
promise.cancel();
```### Clearing a promise chain
Sometimes it is helpful to just clear all the `.then`, and `.catch` promise chains. This method clears whatever has been set in the past.
```javascript
promise.clear();
```## Common questions
### Will a cancellable promise still work with `Promise.race`, and `Promise.all`?Yes. This extension literally `extends Promise`.
### When I call cancel on a promise, will it cause an error in the browser?
`.cancel` internally calls the rejection callback and so it will default to however the browser treats a rejected promise for ES6. Chrome for example will throw an error if a rejection is called on a promise, and there is no `.catch`. Firefox and Safari on the other hand, will not.