@jcoreio/abortable
Version:
memory-leak-proof function to wrap a promise to reject when a signal is aborted
26 lines (17 loc) • 1.27 kB
Markdown
# @jcoreio/abortable
memory-leak-proof function to wrap a promise to reject when a signal is aborted
[](https://circleci.com/gh/jcoreio/abortable)
[](https://codecov.io/gh/jcoreio/abortable)
[](https://github.com/semantic-release/semantic-release)
[](https://badge.fury.io/js/%40jcoreio%2Fabortable)
## `abortable(promise, signal)`
Creates a promise that fulfills when the given `promise` fulfills, or rejects when the given `signal` is aborted,
whichever comes first. If the signal is aborted, rejects with a `DOMException` with `name: 'AbortError'`.
Once the returned promise resolves or rejects, references to all promise and signal handlers will have been removed,
so that it doesn't unexpectedly retain any memory.
```ts
import { abortable } from '@jcoreio/abortable'
const abortController = new AbortContorller()
const { signal } = abortController
const promise = abortable(new Promise((r) => setTimeout(r, 10000)), signal)
```