forked from ProjectEvergreen/greenwood
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync-utils.js
More file actions
33 lines (25 loc) · 754 Bytes
/
Copy pathasync-utils.js
File metadata and controls
33 lines (25 loc) · 754 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
25
26
27
28
29
30
31
32
33
// https://stackoverflow.com/a/76974728/417806
// Constraint: callback functions must not depend on each other
async function asyncFilter(arr, cb) {
const filtered = [];
await asyncForEach(arr, async (element) => {
const needAdd = await cb(element);
if (needAdd) {
filtered.push(element);
}
});
return filtered;
}
// https://stackoverflow.com/a/71278238/417806
// Constraint: mapper functions must not depend on each other
async function asyncMap(items, mapper) {
const promises = [];
for (const item of items) {
promises.push(mapper(item));
}
return Promise.all(promises);
}
async function asyncForEach(items, callback) {
await asyncMap(items, callback);
}
export { asyncFilter, asyncMap, asyncForEach };