Skip to content

Commit

Permalink
Fix promise utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Exelord committed Jan 28, 2023
1 parent 5947f76 commit 58fe192
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/utils/promise.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
export function cancellablePromise<T>(
export async function cancellablePromise<T>(
signal: AbortSignal,
promise: Promise<T>
) {
signal.throwIfAborted();

return Promise.race([
new Promise<never>((_resolve, reject) => {
signal.addEventListener("abort", () => {
reject(signal.reason);
});
signal.addEventListener(
"abort",
() => {
reject(signal.reason);
},
{ once: true, passive: true }
);
}),
promise,
]);
Expand Down

0 comments on commit 58fe192

Please sign in to comment.