promise-abortable
Concept
abort != reject
Features
- Abort in promise
- Abort in promise chain
- Abort for nesting promise
- Return promise after abort
Use Cases
- Cancel request when component hide, unmount or destory
- Cancel long-running async operation
- Return promise with abort for common request function
Browser Support
Any browser that supports Promise.
33 ✔ | 29 ✔ | 8 ✔ | 20 ✔ | 12 ✔ |
- Use Babel for lower versions
- Or include script
iife.es3.js
below - But I think bluebird 3 is a better choice
Install
$ npm install promise-abortable
;
The IIFE build is also available on unpkg:
<!-- 1KB, recommend --> <!-- 1KB --> <!-- 16KB -->
Usage
// 1. Instantiateconst promise = { // 2. Set abort handler signal { // 4. Abort won't reject, but you can reject manually };};// 3. Invoke `signal.onabort(reason)`promise;
Pseudo code
See full examples here.
Abort in promise
const promise = ...;// or: const promise = AbortablePromise.resolve(...);// or: const promise = AbortablePromise.reject(...);// or: const promise = AbortablePromise.all([...]);// or: const promise = AbortablePromise.race([...]);promise;
Abort in promise chain
const promise = ...;promise;
Abort for nesting promise
const promise = AbortablePromise;promise;
Return promise after abort
const promise = ...;promise;
Abort in async/await
const promise = ...;async { try await promise; catch error ...};promise;