Skip to content

Instantly share code, notes, and snippets.

@jakearchibald
Created June 12, 2020 09:53
Show Gist options
  • Save jakearchibald/070c108c65e6db14db43d90d1c3a0305 to your computer and use it in GitHub Desktop.
Save jakearchibald/070c108c65e6db14db43d90d1c3a0305 to your computer and use it in GitHub Desktop.
async function abortable(signal, promise) {
if (signal.aborted) throw new DOMException('AbortError', 'AbortError');
return Promise.race([
promise,
new Promise((_, reject) => {
signal.addEventListener('abort', () =>
reject(new DOMException('AbortError', 'AbortError')),
);
}),
]);
}
async function abortable<T>(
signal: AbortSignal,
promise: Promise<T>,
): Promise<T> {
if (signal.aborted) throw new DOMException('AbortError', 'AbortError');
return Promise.race([
promise,
new Promise<T>((_, reject) => {
signal.addEventListener('abort', () =>
reject(new DOMException('AbortError', 'AbortError')),
);
}),
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment