import asyncForEachStrict from './forEach_strict.js'; function asyncReduce( arr: T[], fn: (previousValue: T, currentValue: T, currentIndex: number) => Promise, ): Promise; function asyncReduce( arr: T[], fn: (previousValue: T, currentValue: T, currentIndex: number) => Promise, initialValue: T, ): Promise; function asyncReduce( arr: T[], fn: (previousValue: U, currentValue: T, currentIndex: number) => Promise, initialValue: U, ): Promise; function asyncReduce( arr: T[], fn: (previousValue: T | U | undefined, currentValue: T, currentIndex: number) => Promise, initialValue?: T | U, ): Promise { let temp = initialValue; return new Promise((resolve, reject) => { asyncForEachStrict( arr, (cur, idx) => new Promise((resolve2, reject2) => { fn(temp, cur, idx) .then((result) => { temp = result; resolve2(); }) .catch(reject2); }), ) .then(() => { resolve(temp); }) .catch(reject); }); } export default asyncReduce;