-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathPromiseAllsettled-Polyfill.js
138 lines (118 loc) Β· 3.74 KB
/
PromiseAllsettled-Polyfill.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* π‘"JavaScript-with-JC"
π Promise.allSettled() and Its Polyfill
Promise.allSettled() returns a promise that gets resolved when all passed promises are settled ( either fulfilled or rejected ) and in result
it gives an array of objects having status and the value/reason of each promise.
π‘ Note :- If passed empty [], returns empty [].
π We can create our own custom allSettled( Polyfill of allSettled ), Check out the code below.π
*/
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.allSettled([p1, p2, p3, p4]).then((result) => {
console.log("result", result);
});
// Using Promise.allSettled() with await
(async () => {
const result = await Promise.allSettled([p1, p2, p3, p4]);
console.log("result", result);
})();
// π‘ Polyfill of Promise.allSettled()
// π 1) Using simple for loop
Promise.customAllSettled = function (promisesArray) {
// π return a new promise
return new Promise((resolve) => {
const result = [];
// π to check how many promises are settled
let settledCount = 0;
// π if passed as empty [] then return empty []
if (promisesArray.length === 0) {
resolve(result);
}
// π if all the promises are settled,
//resolve and return the result array
function resolveFinalResult() {
settledCount++;
if (settledCount === promisesArray.length) {
resolve(result);
}
}
for (let i = 0; i < promisesArray.length; i++) {
Promise.resolve(promisesArray[i])
.then((response) => {
// π if promise passes store its status and increment the count
result[i] = { status: "fulfilled", value: response };
resolveFinalResult();
})
.catch((error) => {
// π if promise fails store its status and increment the count
result[i] = { status: "rejected", reason: error };
resolveFinalResult();
});
}
});
};
Promise.customAllSettled([p1, p2, p3, p4]).then((result) => {
console.log("result custom", result);
});
// Using Promise.allSettled() with await
(async () => {
const result = await Promise.customAllSettled([p1, p2, p3, p4]);
console.log("result custom", result);
})();
/* Output π
[
{ status: 'fulfilled', value: '1st Promise resolved!' },
{ status: 'fulfilled', value: '2nd Promise resolved!' },
{ status: 'fulfilled', value: 3 },
{ status: 'rejected', reason: '4th Promise rejected!' }
]
*/
// π 2) Using map method and promise.all()
Promise.customAllSettled2 = function (promisesArray) {
const transformedpromises = promisesArray.map((promise) => {
return Promise.resolve(promise)
.then((value) => {
return {
status: "fulfilled",
value,
};
})
.catch((reason) => {
return {
status: "rejected",
reason,
};
});
});
return Promise.all(transformedpromises);
};
Promise.customAllSettled2([p1, p2, p3, p4]).then((result) => {
console.log("result custom using map", result);
});
// Using Promise.allSettled() with await
(async () => {
const result = await Promise.customAllSettled2([p1, p2, p3, p4]);
console.log("result custom using map", result);
})();
/* Output π
[
{ status: 'fulfilled', value: '1st Promise resolved!' },
{ status: 'fulfilled', value: '2nd Promise resolved!' },
{ status: 'fulfilled', value: 3 },
{ status: 'rejected', reason: '4th Promise rejected!' }
]
*/