@nextgis/cancelable-promise
Version:
100 lines (68 loc) • 2.15 kB
Markdown
//img.shields.io/bundlephobia/minzip/@nextgis/cancelable-promise) 
A promise you can stop.
Simply download and include with a script tag, `CancelablePromise` will be registered as a global variable.
```html
<script src="../lib/cancelable-promise.global.js"></script>
<script>
var promise = new CancelablePromise((resolve, reject, onCancel) => {
setTimeout(resolve, 1000);
onCancel(() => console.log('canceled'));
});
promise.cancel();
</script>
```
unpkg
```html
<script src="https://unpkg.com/@nextgis/cancelable-promise"></script>
```
jsdelivr
```html
<script src="https://cdn.jsdelivr.net/npm/@nextgis/cancelable-promise"></script>
```
We recommend linking to a specific version number `/cancelable-promise@[version]`
```bash
npm install @nextgis/cancelable-promise
```
Catch `CancelError`
```javascript
import CancelablePromise from "@nextgis/cancelable-promise";
const promise = new CancelablePromise((resolve, reject) => {
setTimeout(() => resolve(), 100);
}).catch((er) => {
if (er.name === "CancelError") {
// handle cancel error
}
throw er;
});
promise.cancel();
```
Handle `onCancel` callback
```javascript
import CancelablePromise from "@nextgis/cancelable-promise";
const promise = new CancelablePromise((resolve, reject, onCancel) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = () => {
resolve(xhr.responseText);
};
xhr.onerror = (er) => {
reject(er);
};
onCancel(() => {
xhr.abort();
});
xhr.send();
});
promise.cancel();
```
Check out the [API Documentation](https://code-api.nextgis.com/modules/_nextgis_cancelable_promise.html)
Need to fix a bug or add a feature to `@nextgis/cancelable-promise`? We provide custom development and support for this software. [Contact us](http://nextgis.com/contact/) to discuss options!
[](http://nextgis.com)
![size](https: