Skip to content

Commit 4625fdc

Browse files
committed
Refactor asyncReduce not to use async
1 parent f8914bf commit 4625fdc

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/reduce.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
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';
62

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;
105

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+
});
1214
}

0 commit comments

Comments
 (0)