-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
24 lines (20 loc) · 545 Bytes
/
Copy pathindex.js
File metadata and controls
24 lines (20 loc) · 545 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
export default async function pReduce(iterable, reducer, initialValue) {
return new Promise((resolve, reject) => {
const iterator = iterable[Symbol.iterator]();
let index = 0;
const next = async total => {
const element = iterator.next();
if (element.done) {
resolve(total);
return;
}
try {
const [resolvedTotal, resolvedValue] = await Promise.all([total, element.value]);
next(reducer(resolvedTotal, resolvedValue, index++));
} catch (error) {
reject(error);
}
};
next(initialValue);
});
}