14 lines
407 B
JavaScript
14 lines
407 B
JavaScript
|
|
//#region src/array/reduceAsync.ts
|
||
|
|
async function reduceAsync(array, reducer, initialValue) {
|
||
|
|
let startIndex = 0;
|
||
|
|
if (initialValue == null) {
|
||
|
|
initialValue = array[0];
|
||
|
|
startIndex = 1;
|
||
|
|
}
|
||
|
|
let accumulator = initialValue;
|
||
|
|
for (let i = startIndex; i < array.length; i++) accumulator = await reducer(accumulator, array[i], i, array);
|
||
|
|
return accumulator;
|
||
|
|
}
|
||
|
|
//#endregion
|
||
|
|
exports.reduceAsync = reduceAsync;
|