UNPKG

softkave-js-utils

Version:

JavaScript & Typescript utility functions, types, and classes

30 lines 1.04 kB
import assert from 'assert'; import { kLoopAsyncSettlementType } from './types.js'; /** * See {@link loopAndCollate} * Returns a list containing results of `fn` invocations */ export async function loopAndCollateAsync(fn, max, settlement, ...otherParams) { assert(max >= 0); if (settlement === kLoopAsyncSettlementType.oneByOne) { const result = Array(max); for (let i = 0; i < max; i++) { result[i] = await fn(i, ...otherParams); } return result; } else { const promises = Array(max); for (let i = 0; i < max; i++) { promises[i] = fn(i, ...otherParams); } if (settlement === kLoopAsyncSettlementType.all) { return (await Promise.all(promises)); } else if (settlement === kLoopAsyncSettlementType.allSettled) { return (await Promise.allSettled(promises)); } } throw new Error(`Unknown promise settlement type ${settlement}`); } //# sourceMappingURL=loopAndCollateAsync.js.map