|
1 | | -export default async function asyncReduce(arr, fn, initialValue) { |
2 | | - let temp = initialValue; |
3 | | - |
4 | | - for (let idx = 0; idx < arr.length; idx += 1) { |
5 | | - const cur = arr[idx]; |
| 1 | +import asyncForEachStrict from './forEach_strict'; |
6 | 2 |
|
7 | | - // eslint-disable-next-line no-await-in-loop |
8 | | - temp = await fn(temp, cur, idx); |
9 | | - } |
| 3 | +export default function asyncReduce(arr, fn, initialValue) { |
| 4 | + let temp = initialValue; |
10 | 5 |
|
11 | | - return temp; |
| 6 | + return new Promise((resolve) => { |
| 7 | + // eslint-disable-next-line no-shadow |
| 8 | + asyncForEachStrict(arr, (cur, idx) => new Promise((resolve2) => { |
| 9 | + // eslint-disable-next-line no-await-in-loop |
| 10 | + fn(temp, cur, idx).then((result) => { temp = result; resolve2(); }); |
| 11 | + })) |
| 12 | + .then(() => { resolve(temp); }); |
| 13 | + }); |
12 | 14 | }
|
0 commit comments