Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/async-array-utils. A collection of array-related async utilities.
array array-methods async promise utils
Something went wrong. Try again.
1.2 kB · 44 lines
TypeScript
at vitest-5
123456789101112131415161718192021222324252627282930313233343536373839404142434445import asyncForEachStrict from './forEach_strict.js';
function asyncReduce<T>( arr: T[], fn: (previousValue: T, currentValue: T, currentIndex: number) => Promise<T>,): Promise<T>;function asyncReduce<T>( arr: T[], fn: (previousValue: T, currentValue: T, currentIndex: number) => Promise<T>, initialValue: T,): Promise<T>;function asyncReduce<T, U>( arr: T[], fn: (previousValue: U, currentValue: T, currentIndex: number) => Promise<U>, initialValue: U,): Promise<U>;function asyncReduce<T, U>( arr: T[], fn: (previousValue: T | U | undefined, currentValue: T, currentIndex: number) => Promise<T | U>, initialValue?: T | U,): Promise<T | U | undefined> { let temp = initialValue;
return new Promise((resolve, reject) => { asyncForEachStrict( arr, (cur, idx) => new Promise<void>((resolve2, reject2) => { fn(temp, cur, idx) .then((result) => { temp = result; resolve2(); }) .catch(reject2); }), ) .then(() => { resolve(temp); }) .catch(reject); });}
export default asyncReduce;