@freeword/meta
Version:
Meta package for Freeword: exports all core types, constants, and utilities from the src/ directory.
49 lines • 1.89 kB
JavaScript
import _ /**/ from 'lodash';
import { throwable } from "./OutcomeUtils.js";
export async function AwaitTasks(coll, func, { parallelism = 4 } = {}, otherArgs = []) {
const keys = _.isArray(coll) ? _.range(coll.length) : _.keys(coll);
const items = _.values(coll);
// Validate.AwaitTasks({ items, func, parallelism, otherArgs })
let idx = 0;
const workerIDs = _.range(parallelism);
const tasks = [];
//
const workerTasks = Promise.all(_.map(workerIDs, async (_workerID) => {
while (idx < items.length) {
const ptr = idx;
idx += 1;
const item = items[ptr];
tasks[ptr] = await func(item, keys[ptr], ptr, ...otherArgs);
}
return tasks;
}));
await workerTasks;
return tasks;
}
export async function PromiseWalk(coll, func = _.identity, opts = [], otherArgs = []) {
if (_.isMap(coll)) {
throw throwable('no walkies for Map type yet', 'notYet', coll);
}
if (_.isArray(coll)) {
const result = AwaitTasks(coll, func, opts, otherArgs);
return result;
}
const result = await AwaitTasks(coll, func, opts, otherArgs);
return _.zipObject(_.keys(coll), result);
}
export async function AwaitBag(obj, opts = {}) {
const results = PromiseWalk(obj, _.identity, opts);
return results;
}
// // Applies a function returning pairs [key, val] with given parallelism
// // returns a bag using the given key/val pairs
// //
// // Returned items will always be in the same order at the input collection.
// // Tasks will be started in order but there are no other guarantees
// // about the timing or sequence of tasks.
// //
// export async function PromiseBagify(coll, func, opts = {}, otherArgs = []) {
// const result = await AwaitTasks(coll, func, opts, otherArgs)
// return _.fromPairs(result)
// }
//# sourceMappingURL=PromiseUtils.js.map