-
-
Save jakearchibald/070c108c65e6db14db43d90d1c3a0305 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')), | |
); | |
}), | |
]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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