# make-cancellable-promise

> Make any Promise cancellable.

Latest version **2.0.0** (published 2025-03-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install make-cancellable-promise
pnpm add make-cancellable-promise
yarn add make-cancellable-promise
bun add make-cancellable-promise
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2025-03-20 |
| First published | 2019-07-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 31 |
| Author | Wojciech Maj |
| Maintainers | wojtekmaj |
| Keywords | promise, promise-cancelling |

## Links

- npm: https://www.npmjs.com/package/make-cancellable-promise
- Repository: https://github.com/wojtekmaj/make-cancellable-promise
- Homepage: https://github.com/wojtekmaj/make-cancellable-promise#readme
- Issues: https://github.com/wojtekmaj/make-cancellable-promise/issues
- Funding: https://github.com/wojtekmaj/make-cancellable-promise?sponsor=1
- npm.io page: https://npm.io/package/make-cancellable-promise

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 2.0.0 (latest) — 2025-03-20
- 1.3.2 — 2023-10-18
- 1.3.1 — 2023-05-29
- 1.3.0 — 2023-05-29
- 1.2.1 — 2023-02-02
- 1.2.0 — 2023-01-10
- 1.1.0 — 2021-09-13
- 1.0.0 — 2019-07-17

## README

[![npm](https://img.shields.io/npm/v/make-cancellable-promise.svg)](https://www.npmjs.com/package/make-cancellable-promise) ![downloads](https://img.shields.io/npm/dt/make-cancellable-promise.svg) [![CI](https://github.com/wojtekmaj/make-cancellable-promise/actions/workflows/ci.yml/badge.svg)](https://github.com/wojtekmaj/make-cancellable-promise/actions)

# Make-Cancellable-Promise

Make any Promise cancellable.

## tl;dr

- Install by executing `npm install make-cancellable-promise` or `yarn add make-cancellable-promise`.
- Import by adding `import makeCancellablePromise from 'make-cancellable-promise`.
- Do stuff with it!
  ```ts
  const { promise, cancel } = makeCancellablePromise(myPromise);
  ```

## User guide

### makeCancellablePromise(myPromise)

A function that returns an object with two properties:

`promise` and `cancel`. `promise` is a wrapped around your promise. `cancel` is a function which stops `.then()` and `.catch()` from working on `promise`, even if promise passed to `makeCancellablePromise` resolves or rejects.

#### Usage

```ts
const { promise, cancel } = makeCancellablePromise(myPromise);
```

Typically, you'd want to use `makeCancellablePromise` in React components. If you call `setState` on an unmounted component, React will throw an error.

Here's how you can use `makeCancellablePromise` with React:

```tsx
function MyComponent() {
  const [status, setStatus] = useState('initial');

  useEffect(() => {
    const { promise, cancel } = makeCancellable(fetchData());

    promise.then(() => setStatus('success')).catch(() => setStatus('error'));

    return () => {
      cancel();
    };
  }, []);

  const text = (() => {
    switch (status) {
      case 'pending':
        return 'Fetching…';
      case 'success':
        return 'Success';
      case 'error':
        return 'Error!';
      default:
        return 'Click to fetch';
    }
  })();

  return <p>{text}</p>;
}
```

## License

The MIT License.

## Author

<table>
  <tr>
    <td >
      <img src="https://avatars.githubusercontent.com/u/5426427?v=4&s=128" width="64" height="64" alt="Wojciech Maj">
    </td>
    <td>
      <a href="https://github.com/wojtekmaj">Wojciech Maj</a>
    </td>
  </tr>
</table>

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