@jcoreio/abortable
v1.0.2
Published
memory-leak-proof function to wrap a promise to reject when a signal is aborted
Downloads
194
Readme
@jcoreio/abortable
memory-leak-proof function to wrap a promise to reject when a signal is aborted
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.
import { abortable } from '@jcoreio/abortable'
const abortController = new AbortContorller()
const { signal } = abortController
const promise = abortable(new Promise((r) => setTimeout(r, 10000)), signal)