-
Notifications
You must be signed in to change notification settings - Fork 28
/
bundle.ts
35 lines (32 loc) · 1.13 KB
/
bundle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import callAll from "./callAll.ts";
import type { λ } from "./types.ts";
/**
* Given a list of functions that accept the same parameters, returns a function
* that takes these parameters and invokes all of the given functions.
*
* The returned function returns a promise that resolves once all functions
* returned/resolved and rejects if any of the functions throws/rejects - but
* only after all returned promises have been settled.
*/
const bundle =
<T extends unknown[]>(...funs: (λ<T> | undefined)[]) =>
async (...args: T): Promise<void> => {
const res = await Promise.allSettled(
funs.map((f) => (async () => await f?.(...args))()),
);
res.forEach((v) => {
if (v.status === "rejected") throw v.reason;
});
};
/**
* Same as {@link bundle}, but return synchronously.
*
* If any of the functions throws an error synchronously, none of the functions
* after it will be invoked and the error will propagate.
*/
export const bundleSync = <T extends unknown[]>(
...funs: (λ<T> | undefined)[]
) =>
(...args: T) =>
void callAll(funs.filter((f) => f !== undefined) as λ<T>[], ...args);
export default bundle;