froebel
Version:
TypeScript utility library
40 lines (32 loc) • 1.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.bundleSync = void 0;
var _callAll = _interopRequireDefault(require("./callAll"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* 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.
*/
const bundleSync = (...funs) => (...args) => void (0, _callAll.default)(funs.filter(f => f !== undefined), ...args);
exports.bundleSync = bundleSync;
var _default = bundle;
exports.default = _default;
module.exports = Object.assign(exports.default || {}, exports);