-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromiseAll.js
48 lines (46 loc) · 1.09 KB
/
promiseAll.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
function promiseAll(array) {
let count = 0;
const len = array.length;
const result = new Array(len);
return new Promise((resolve, reject) => {
for (let index = 0; index < array.length; index++) {
Promise.resolve(array[index]).then(
(value) => {
result[index] = value;
count++;
if (count === array.length) {
resolve(result);
}
},
(value) => {
reject(value);
}
);
}
});
}
var p1 = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, "one");
});
var p2 = new Promise((resolve, reject) => {
setTimeout(resolve, 2000, "two");
});
var p3 = new Promise((resolve, reject) => {
setTimeout(resolve, 3000, "three");
});
var p4 = new Promise((resolve, reject) => {
setTimeout(resolve, 4000, "four");
});
var p5 = new Promise((resolve, reject) => {
// reject('reject');
setTimeout(resolve, 11, "5");
});
promiseAll([p1, p2, p3, p4, p5]).then(
// Promise.all([p1, p2, p3, p4, p5]).then(
(values) => {
console.log(values);
},
(reason) => {
console.log(reason);
}
);