abort-controller-x
Version:
Abortable async function helpers
25 lines (21 loc) • 547 B
text/typescript
import {execute} from './execute';
/**
* Wrap a promise to reject with `AbortError` once `signal` is aborted.
*
* Useful to wrap non-abortable promises.
* Note that underlying process will NOT be aborted.
*/
export function abortable<T>(
signal: AbortSignal,
promise: PromiseLike<T>,
): Promise<T> {
if (signal.aborted) {
// prevent unhandled rejection
const noop = () => {};
promise.then(noop, noop);
}
return execute<T>(signal, (resolve, reject) => {
promise.then(resolve, reject);
return () => {};
});
}