softkave-js-utils
Version:
JavaScript & Typescript utility functions, types, and classes
27 lines • 893 B
JavaScript
import assert from 'assert';
import { kLoopAsyncSettlementType } from './types.js';
/** See {@link loop} */
export async function loopAsync(fn, max, settlement, ...otherParams) {
assert(max >= 0);
if (settlement === kLoopAsyncSettlementType.oneByOne) {
for (let i = 0; i < max; i++) {
await fn(i, ...otherParams);
}
}
else {
const promises = Array(max);
for (let i = 0; i < max; i++) {
promises.push(fn(i, ...otherParams));
}
if (settlement === kLoopAsyncSettlementType.all) {
await Promise.all(promises);
}
else if (settlement === kLoopAsyncSettlementType.allSettled) {
await Promise.allSettled(promises);
}
else {
throw new Error(`Unknown promise settlement type ${settlement}`);
}
}
}
//# sourceMappingURL=loopAsync.js.map