-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (48 loc) · 1.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
const {argFoo, foo} = factorList[ix]
*/
const pNeAsync = async (list, factorList, failFast= true) => {
const errors = [];
const result = [];
async function recur(data, acc, ix = 0){
const {argFoo, foo} = factorList[ix];
await Promise.all(data.map(async datum => {
let res;
try{
res = await foo(argFoo(datum));
} catch(err){
if(failFast){
throw err;
}
errors.push({
object: datum,
depth: ix,
argFoo,
foo,
error: err
});
acc.push({
parent: datum,
children: 'error'
});
return;
}
if(ix === factorList.length - 1){
acc.push({
parent: datum,
children: res
});
} else {
acc.push({
parent: datum,
children: []
});
const newAcc = acc[acc.length -1].children;
await(recur(res, newAcc, ix + 1));
}
}));
}
await recur(list, result);
return [result, errors];
};
module.exports = pNeAsync;