abort != reject
- Abort in promise
- Abort in promise chain
- Abort for nesting promise
- Return promise after abort
- Cancel request when component hide, unmount or destory
- Cancel long-running async operation
- Return promise with abort for common request function
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
$ npm install promise-abortable
import AbortablePromise from "promise-abortable";
// For Node 6+
const AbortablePromise = require("promise-abortable");
// For Node 4-
var AbortablePromise = require("promise-abortable/dist/cjs.es5.js");
The IIFE build is also available on unpkg:
<script src="https://unpkg.com/promise-abortable/dist/iife.es5.js"></script> <!-- 1KB, recommend -->
<script src="https://unpkg.com/promise-abortable/dist/iife.es6.js"></script> <!-- 1KB -->
<script src="https://unpkg.com/promise-abortable/dist/iife.es3.js"></script> <!-- 16KB -->
// 1. Instantiate
const promise = new AbortablePromise((resolve, reject, signal) => {
// 2. Set abort handler
signal.onabort = reason => {
// 4. Abort won't reject, but you can reject manually
};
});
// 3. Invoke `signal.onabort(reason)`
promise.abort(reason);
See full examples here.
const promise = new AbortablePromise(...);
// or: const promise = AbortablePromise.resolve(...);
// or: const promise = AbortablePromise.reject(...);
// or: const promise = AbortablePromise.all([...]);
// or: const promise = AbortablePromise.race([...]);
promise.abort();
const promise = new AbortablePromise(...).then(...).catch(...);
promise.abort();
const promise = AbortablePromise.resolve(...).then(value => {
return new AbortablePromise(...);
});
promise.abort();
const promise = new AbortablePromise(...);
promise.abort().then(...).catch(...);
const promise = new AbortablePromise(...);
(async () => {
try { await promise; } catch (error) {...}
})();
promise.abort();