froebel
Version:
TypeScript utility library
26 lines (23 loc) • 1 kB
JavaScript
import callAll from "./callAll.mjs";
/**
* 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 = (...funs) => async (...args) => {
const res = await Promise.allSettled(funs.map(f => (async () => await (f === null || f === void 0 ? void 0 : 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 = (...funs) => (...args) => void callAll(funs.filter(f => f !== undefined), ...args);
export default bundle;