-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallelAsync.js
More file actions
41 lines (34 loc) · 743 Bytes
/
ParallelAsync.js
File metadata and controls
41 lines (34 loc) · 743 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
34
35
36
37
38
39
40
41
// takes async function as input and invoke callback after async function is completed
const p1 = new Promise((resolve) => {
setTimeout(() => {
resolve("P1");
}, 1000);
});
const p2 = new Promise((resolve) => {
setTimeout(() => {
resolve("P2");
}, 2000);
});
const p3 = new Promise((resolve) => {
setTimeout(() => {
resolve("P3");
}, 100);
});
const callback = () => {
console.log("hello");
};
const p4 = [p1, p2, p3];
// or simply use Promise.all
function executeParallel(p4, callback) {
let count = 0;
p4.forEach((promise) => {
promise.then((val) => {
count++;
console.log(val);
if (count === p4.length) {
callback();
}
});
});
}
executeParallel(p4, callback);