Skip to content

Commit

Permalink
feat(CancellablePromise): add the allSettled static method (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturAbdullin authored Jul 10, 2024
1 parent b9ec6e3 commit 478ee1a
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/CancellablePromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ export class CancellablePromise<T = unknown> implements Promise<T> {
);
}

static allSettled<Values extends readonly unknown[] | []>(values: Values) {
function cancel() {
for (const promise of values as CancellablePromise[]) {
promise.cancel();
}
}

return new CancellablePromise(
Promise.allSettled(values) as unknown as Promise<{
-readonly [P in keyof Values]: Awaited<Values[P]>;
}>,
cancel,
);
}

static race<Values extends readonly unknown[] | []>(values: Values) {
function cancel() {
for (const promise of values as CancellablePromise[]) {
Expand Down

0 comments on commit 478ee1a

Please sign in to comment.