Repository navigation
Expand file tree
/
Copy pathPromiseAll-Polyfill.js
More file actions
90 lines (77 loc) · 2.22 KB
/
Copy pathPromiseAll-Polyfill.js
File metadata and controls
90 lines (77 loc) · 2.22 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* 💡"JavaScript-with-JC"
👉 Promise.all() and Its Polyfill
Promise.all() - It executes all passed promises concurrently and improves the performance of the application.
💡Promise.all() Cases :-
1) If all promises resolve, returns the array of results of all promises resolved.
2) If any promise fails, returns the rejected promise error.
3) If passed empty [], returns empty [].
*/
const p1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("1st Promise resolved!");
}, 1000);
});
const p2 = Promise.resolve("2nd Promise resolved!");
const p3 = 3;
const p4 = new Promise((resolve, reject) => {
setTimeout(() => {
let status = true;
if (status) {
resolve("4th Promise resolved!");
} else {
reject("4th Promise rejected!");
}
}, 2000);
});
Promise.all([p1, p2, p3, p4])
.then((result) => {
console.log("result all", result);
})
.catch((error) => {
console.log("error all", error);
});
//💡Polyfill of Promise.all
Promise.customAll = function (promisesArray) {
// 👇 return a new promise
return new Promise((resolve, reject) => {
const result = [];
// 👇 to check how many promises are completed
let completed = 0;
// 👇 if passed as empty [] then return empty []
if (promisesArray.length === 0) {
resolve(result);
}
// 👇 execute each promise of promisesArray
for (let i = 0; i < promisesArray.length; i++) {
Promise.resolve(promisesArray[i])
.then((response) => {
// 👇 if promise passes store its response and increment the count
result[i] = response;
completed++;
// 👇 if all the promises are completed,
//resolve and return the result array
if (completed === promisesArray.length) {
resolve(result);
}
})
.catch((error) => {
// 👇 if any promise fails, reject.
reject(error);
});
}
});
};
Promise.customAll([p1, p2, p3, p4])
.then((result) => {
console.log("result customAll", result);
})
.catch((error) => {
console.log("error customAll", error);
});
/* output :-
[
"1st Promise resolved!",
"2nd Promise resolved!",
3,
"4th Promise resolved!",
]; */