https://github.com/jcoreio/abortable
https://github.com/jcoreio/abortable
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/jcoreio/abortable
- Owner: jcoreio
- License: mit
- Created: 2024-11-12T00:48:02.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-12-03T02:32:00.000Z (over 1 year ago)
- Last Synced: 2025-01-01T01:38:05.146Z (over 1 year ago)
- Language: TypeScript
- Size: 61.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# @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)
```