Make promises abortable! That's it! :)
npm install make-abortableMake promises abortable! That's it! :)
``bash`if using npm
npm i make-abortableif using yarn
yarn add make-abortable
`js
const abortable = require('make-abortable');
const controller = new AbortController();
const signal = controller.signal;
const promise = new Promise(resolve => {
setTimeout(() => {
resolve();
}, 1000);
});
// abortable(promise, controller) works as well
const abortablePromise = abortable(promise, { signal });
// abort the promise
controller.abort();
abortablePromise
.then(() => {
// this will not execute
})
.catch((err) => {
if (err.name === 'AbortError') return;
// handle real errors here
});
`
This library utilizes the AbortController api. This api is new so you might need a polyfill. Read about the AbortController` api here.