-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyPromise.js
238 lines (209 loc) · 7.27 KB
/
MyPromise.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
const STATE = {
// states of the promise
FULFILLED: "fulfilled",
REJECTED: "rejected",
PENDING: "pending",
};
class MyPromise {
// we define a constructor and pass a calback func to it which contain our resolve and reject states.
#thencbs = []; // empty array to store the callbacks passed to then.
#catchcbs = []; // empty array to store the callbacks passed to catch.
#state = STATE.PENDING; // info about the current state of the promise (pending, rejected, fulfilled).
#value; // the value passed into the function onSuccess and onFail.
#onSuccessBind = this.#onSuccess.bind(this); // to properly bind the this variable to amke sure its hooked up.
#onFailBind = this.#onFail.bind(this);
constructor(cb) {
// our callback as discussed above will have two states: on Success state and on Failure state.
try {
cb(this.#onSuccessBind, this.#onFailBind); // work as resolve, reject
} catch (error) {
this.#onFail(error);
}
}
#runCallbacks() {
if (this.#state === STATE.FULFILLED) {
this.#thencbs.forEach((cb) => {
cb(this.#value); // run all then cbs for success state
});
}
this.#thencbs = []; // remove all the cbs after execution to avoid re-running them.
if (this.#state === STATE.REJECTED) {
this.#catchcbs.forEach((cb) => {
cb(this.#value); // run all catch cbs for fail state
});
}
this.#catchcbs = []; // remove all the cbs after execution to avoid re-running them.
}
// defining the two states (private methods)
#onSuccess(value) {
queueMicrotask(() => {
if (this.#state !== STATE.PENDING) return; // as we want resolve and reject to run only once in a promise.
// promise can contain more promises inside them so to handle them we call then after checking that it is indeed a instance of a promise.
if (value instanceof MyPromise) {
value.then(this.#onSuccessBind, this.#onFailBind);
return;
}
this.#value = value;
this.#state = STATE.FULFILLED; // changing states as per success or fail
this.#runCallbacks();
});
}
#onFail(value) {
queueMicrotask(() => {
if (this.#state !== STATE.PENDING) return; // as we want resolve and reject to run only once in a promise.
// promise can contain more promises inside them so to handle them we call then after checking that it is indeed a instance of a promise.
if (value instanceof MyPromise) {
value.then(this.#onSuccessBind, this.#onFailBind);
return;
}
// when the code gives a error but no catch is provided.
if (this.#catchcbs.length === 0) {
throw new UncaughtPromiseError(value);
}
this.#value = value;
this.#state = STATE.REJECTED; // changing states as per success or fail
this.#runCallbacks();
});
}
// Promises contain public methods like then, catch, finally...
then(thencb, catchcb) {
// contains a callback with next set of actions to be performed.
// we define an array of callbacks because a promise can have multiple callback uppon successful execution.
return new MyPromise((resolve, reject) => {
// push callback on to the array for execution on success state.
this.#thencbs.push(result => {
// in case we dont care avobt the successtate, and only care about fail state, or in other words our thencb is null we directly resolve the promise.
if (thencb == null) {
resolve(result);
return;
}
// but if thencb is not null.
try {
resolve(thencb(result));
} catch (error) {
reject(error);
}
});
// push callback on to the array for execution on fail state.
this.#catchcbs.push(result => {
// in case we dont care avobt the rejectstate, and only care about success state, or in other words our catchcb is null we directly reject the promise.
if (catchcb == null) {
reject(result);
return;
}
// but if catchcb is not null.
try {
resolve(catchcb(result));
} catch (error) {
reject(error);
}
});
this.#runCallbacks(); // when there are still callbacks left to run even after the state is changed to fullfilled.
});
}
catch(cb) {
// calling the then method with thencb as undefined and catchcb as given cb.
return this.then(undefined, cb);
}
finally(cb) {
return this.then(
(result) => {
cb();
return result;
},
(result) => {
cb();
throw result;
}
);
}
static resolve(value) {
// resolve the promise
return new Promise((resolve, reject) => {
resolve(value);
});
}
static reject(value) {
// reject the promise
return new Promise((resolve, reject) => {
reject(value);
});
}
// static func to resolve all promises in the passed promises array and return the array of results.
// if any one promise fails we immediately reject.
static all(promises) {
const results = [];
let completedPromises = 0;
return new MyPromise((resolve, reject) => {
for (let i = 0; i < promises.length; i++) {
const promise = promises[i];
promise
.then((value) => {
completedPromises++;
results[i] = value;
if (completedPromises === promises.length) {
resolve(results);
}
})
.catch(reject);
}
});
}
// static func to resolve all promises in the passed promises array and return the array of results.
// but here if we resolve everyPromise and store value for success and reason for failure.
static allSettled(promises) {
const results = [];
let completedPromises = 0;
return new MyPromise((resolve) => {
for (let i = 0; i < promises.length; i++) {
const promise = promises[i];
promise
.then((value) => {
results[i] = { status: STATE.FULFILLED, value };
})
.catch((reason) => {
results[i] = { status: STATE.REJECTED, reason };
})
.finally(() => {
completedPromises++;
if (completedPromises === promises.length) {
resolve(results);
}
});
}
});
}
// returns the first promise that runs in success state.
static race(promises) {
return new MyPromise((resolve, reject) => {
promises.forEach((promise) => {
promise.then(resolve).catch(reject);
});
});
}
// returns the set of all promises that resturn a fail state.
static any(promises) {
const errors = [];
let rejectedPromises = 0;
return new MyPromise((resolve, reject) => {
for (let i = 0; i < promises.length; i++) {
const promise = promises[i];
promise.then(resolve).catch((value) => {
rejectedPromises++;
errors[i] = value;
if (rejectedPromises === promises.length) {
reject(new AggregateError(errors, 'All promises were rejected'));
}
});
}
});
}
}
// Custom error class to throw error in case any promise gives error.
class UncaughtPromiseError extends Error {
constructor(error) {
super(error);
this.stack = `(in promise) ${error.stack}`;
}
}
module.exports = MyPromise;