abort-utils
Version:
Utility functions to use and combine `AbortSignal` and `AbortController` with Promises
21 lines (20 loc) • 536 B
JavaScript
async function watch(promise, controller) {
try {
await promise;
}
catch {
// https://twitter.com/fregante/status/1777377935311213024
}
finally {
controller.abort();
}
}
/**
* @param promise The promise to watch for resolution or rejection.
* @returns An `AbortSignal` that aborts when the promise is resolved or rejected
*/
export function signalFromPromise(promise) {
const controller = new AbortController();
void watch(promise, controller);
return controller.signal;
}