# @nextgis/cancelable-promise

> A promise you can stop

Latest version **4.0.2** (published 2026-11-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install @nextgis/cancelable-promise
pnpm add @nextgis/cancelable-promise
yarn add @nextgis/cancelable-promise
bun add @nextgis/cancelable-promise
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.0.2 |
| Published | 2026-11-05 |
| First published | 2020-03-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 199.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 31 |
| Author | NextGIS |
| Maintainers | rendrom, maximdubinin, karavanjo |
| Keywords | NextGIS, MAP |

## Links

- npm: https://www.npmjs.com/package/@nextgis/cancelable-promise
- Repository: https://github.com/nextgis/nextgis_frontend
- Homepage: https://github.com/nextgis/nextgis_frontend/tree/master/packages/cancelable-promise#readme
- Issues: https://github.com/nextgis/nextgis_frontend/issues
- npm.io page: https://npm.io/package/@nextgis/cancelable-promise

## Recent versions

- 4.0.2 (latest) — 2026-11-05
- 4.0.1 — 2026-11-02
- 4.0.0 — 2026-08-30
- 4.0.0-rc.2 — 2026-08-30
- 4.0.0-rc.1 — 2026-08-29
- 4.0.0-rc.0 — 2026-08-27
- 4.0.0-alpha.8 — 2026-08-25
- 4.0.0-alpha.6 — 2026-08-24
- 4.0.0-alpha.5 — 2026-08-21
- 3.0.0 — 2025-07-26
- 3.0.0-alpha.22 — 2025-05-14
- 3.0.0-alpha.17 — 2024-12-24
- 3.0.0-alpha.13 — 2024-10-24
- 3.0.0-alpha.5 — 2024-09-12
- 3.0.0-alpha.3 — 2024-07-30
- … 73 more at https://npm.io/package/@nextgis/cancelable-promise/versions

## README

# CancelablePromise

![version](https://img.shields.io/npm/v/@nextgis/cancelable-promise)

A promise you can stop.

## Installation

### In Browser

#### Include assets

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>
```

#### CDN

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]`

### In Node.js

```bash
npm install @nextgis/cancelable-promise
```

## Usage

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)

## 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](http://nextgis.com/contact/) to discuss options!

[![http://nextgis.com](https://nextgis.com/img/nextgis.png)](http://nextgis.com)

---
_Source: https://npm.io/package/@nextgis/cancelable-promise · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
