-
Notifications
You must be signed in to change notification settings - Fork 0
/
Promises.js
44 lines (42 loc) · 1.21 KB
/
Promises.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
function Promise_all(promises) {
return new Promise(async(resolve, reject) => {
//Your code here.
// resolve(Promise.all(promises));
let length = promises.length;
if(length <= 0) {
resolve([]);
} else {
let result = [];
for(let i=0; i<promises.length; i++) {
promises[i]
.then(res => {
result[i]=res;
length--;
length===0 && resolve(result)
})
.catch((err)=>reject(err));
}
}
});
}
// Test code.
Promise_all([]).then(array => {
console.log("This should be []:", array);
});
function soon(val) {
return new Promise(resolve => {
setTimeout(() => resolve(val), Math.random() * 500);
});
}
Promise_all([soon(1), soon(2), soon(3)]).then(array => {
console.log("This should be [1, 2, 3]:", array);
}).catch(err => console.log(err));
Promise_all([soon(1), Promise.reject("X"), soon(3)])
.then(array => {
console.log("We should not get here");
})
.catch(error => {
if (error !== "X") {
console.log("Unexpected failure:", error);
}
});