Cancelable Promise
A promise you can stop.
Installation
$ npm install --save-dev @nextgis/cancelable-promise
$ yarn add @nextgis/cancelable-promise
Usage
Catch `CancelError'
import CancelablePromise from "@nextgis/cancelable-promise";
const promise = new CancelablePromise((resolve, reject) => {
setTimeout(() => resolve(), 100);
}).catch((er) => {
if (er.name === "CancelError") {
}
throw er;
});
promise.cancel();
Handle onCancel
callback
import CancelablePromise from "@nextgis/cancelable-promise";
const promise = new CancelablePromise((resolve, reject, onCancel) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = () => {
resolve();
};
xhr.onerror = (er) => {
reject(er);
};
onCancel(() => {
xhr.abort();
});
xhr.send();
});
promise.cancel();
Commercial support
Need to fix a bug or add a feature to @nextgis/cancelable-promise
? We provide custom development and support for this software. Contact us to discuss options!