-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustomPromises.js
170 lines (144 loc) · 3.9 KB
/
customPromises.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Polyfill to implement custom promises
class MyPromise {
resolvedData;
resolveChain = [];
isResolved = false;
rejectedData;
rejectChain = [];
isRejected = false;
constructor(executor) {
const resolve = (value) => {
this.isResolved = true;
this.resolvedData = value;
if (this.resolveChain.length) {
this.resolveChain.reduce((acc, fn) => fn(acc), this.resolvedData);
}
};
const reject = (value) => {
this.rejectedData = value;
this.isRejected = true;
if (this.rejectChain.length) {
this.rejectChain.reduce((acc, fn) => fn(acc), this.rejectedData);
}
};
try {
executor(resolve, reject);
} catch (error) {
reject(error);
}
}
then(fn) {
this.resolveChain.push(fn);
if (this.isResolved) {
this.resolveChain.reduce((acc, fn) => fn(acc), this.resolvedData);
}
return this;
}
catch(fn) {
this.rejectChain.push(fn);
if (this.isRejected) {
this.rejectChain.reduce((acc, fn) => fn(acc), this.rejectedData);
}
return this;
}
finally(fn) {
this.resolveChain.push(fn);
this.rejectChain.push(fn);
if (this.isResolved) {
this.resolveChain.reduce((acc, fn) => fn(acc), this.resolvedData);
}
if (this.isRejected) {
this.rejectChain.reduce((acc, fn) => fn(acc), this.rejectedData);
}
}
static resolve(value) {
return new MyPromise((resolve) => resolve(value));
}
static reject(value) {
return new Promise((_, reject) => reject(value));
}
static all(promises) {
const results = [];
let count = 0;
return new MyPromise((resolve, reject) => {
for (let i = 0; i < promises.length; i++) {
promises[i]
.then((data) => {
results[i] = data;
count++;
if (count === promises.length) {
resolve(results);
}
})
.catch((error) => reject(error));
}
});
}
static race(promises) {
return new MyPromise((resolve, reject) => {
promises.forEach((promise) => {
return promise.then(resolve).catch(reject);
});
});
}
static allSettled(promises) {
const result = [];
let count = 0;
return new Promise((resolve) => {
const handleResult = (value, index, status) => {
result[index] = { status, value };
count++;
if (count === promises.length) resolve(result);
};
for (let i = 0; i < promises.length; i++) {
promises[i]
.then((res) => handleResult(res, i, "fulfilled"))
.catch((err) => handleResult(err, i, "rejected"));
}
});
}
static any(promises) {
const result = [];
let count = 0;
return new Promise((resolve, reject) => {
for (let i = 0; i < promises.length; i++) {
promises[i].then(resolve).catch((err) => {
result[index] = { status: "rejected", value: err };
count++;
if (count === promises.length) reject(result);
});
}
});
}
}
// MyPromise.all([
// Promise.resolve(10),
// Promise.resolve(20),
// Promise.reject("Error"),
// Promise.resolve(40),
// ]).then((data) => console.log(data));
// MyPromise.resolve(10).then((data) => console.log(data));
// MyPromise.reject("Error").catch((data) => console.log(data));
// new MyPromise((resolve, reject) => {
// setTimeout(() => {
// resolve(100);
// }, 100);
// setTimeout(() => {
// reject("Async Error!!");
// }, 100);
// resolve(10);
// reject("Error!");
// });
// .then((data) => {
// return data * 2;
// })
// .then((data) => {
// console.log(data);
// })
// .catch((err) => err)
// .catch((err) => console.log(err));
// .finally((data) => console.log(data));
// Edge case
// 1. Don't allow multiple resolve and reject returns
// 2. Return new promise from .then() or .catch()
// 3. Only return error if the .any() any of the promise fails