-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.js
214 lines (194 loc) · 5.93 KB
/
promise.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
module.exports = class MyPromise {
// 2.1 the states of MyPromise: pending, fulfilled, or rejected
#state;
// a MyPromise’s eventual value/reason
#value;
// an array for fulfilled handler functions
#onFulfilledHandlers;
// an array for rejected handler functions
#onRejectedHandlers;
/**
* @param {Function} func - A handler function with 2 prepared callbacks: `resolve` and `reject`
*
* `resolve`- resolves with a given value
*
* `reject`- rejects with a given reason
*
* @example
*
* const promise = new MyPromise((resolve, reject) => {
* setTimeout(() => resolve('resolved!'), 1000);
* // setTimeout(() => reject('rejected!'), 1000);
* })
* @return {MyPromise}
*/
constructor(func) {
this.#state = 'pending';
this.#onFulfilledHandlers = [];
this.#onRejectedHandlers = [];
try {
this.#resolve(func, this.#fulfill.bind(this), this.#reject.bind(this));
} catch (error) {
this.#reject(error);
}
}
// 2.3
#handleResolution(promise, x, resolve, reject) {
// 2.3.1
if (promise === x) {
reject(new TypeError('Chaining cycle detected for promise'));
return;
}
if (x && (typeof x === 'object' || typeof x === 'function')) {
let thenCalled = false;
try {
// 2.3.3.1
const then = x.then;
// 2.3.3.3
if (typeof then === 'function') {
then.call(
x,
(y) => {
// 2.3.3.3.3
if (thenCalled) return;
thenCalled = true;
// 2.3.3.3.1
this.#handleResolution(promise, y, resolve, reject);
},
(r) => {
// 2.3.3.3.3
if (thenCalled) return;
thenCalled = true;
// 2.3.3.3.2
reject(r);
}
);
return;
}
// 2.3.3.2, 2.3.3.3.4
} catch (error) {
// 2.3.3.3.4.1
if (thenCalled) return;
// for an async resolvePromise/rejectPromise invocation
thenCalled = true;
// 2.3.3.3.4.2
reject(error);
return;
}
}
resolve(x);
}
#fulfill(value) {
// 2.1.2, 2.1.3
if (this.#state !== 'pending') return;
this.#value = value;
this.#state = 'fulfilled';
// 2.2.4
setTimeout(() => {
// 2.2.6.1
this.#onFulfilledHandlers.forEach((fn) => fn());
}, 0);
}
#reject(reason) {
// 2.1.2, 2.1.3
if (this.#state !== 'pending') return;
this.#value = reason;
this.#state = 'rejected';
// 2.2.4
setTimeout(() => {
// 2.2.6.2
this.#onRejectedHandlers.forEach((fn) => fn());
}, 0);
}
#resolve(fn, onFulfilled, onRejected) {
fn(
(value) => {
this.#handleResolution(this, value, onFulfilled, onRejected)
},
(reason) => {
onRejected(reason);
}
);
}
#callCallback(value, callback, resolve, reject) {
try {
// 2.2.5
const result = callback(value);
// 2.2.7.1
resolve(result);
} catch (error) {
// 2.2.7.2
reject(error);
}
}
/**
* If the passed argument is a function, it will be called asynchronously when the current `MyPromise` is settled.
*
* Subscribe to given `MyPromise` instance
* and register 2 callbacks:
* `onFulfilled` and `onRejected`
*
* `onFulfilled(value)` takes the fulfilled value as a parameter from subscribed `MyPromise`. Its return value becomes the fulfillment value of the promise returned by `then()`.
*
* `onRejected(reason)` takes the rejected reason as a parameter from subscribed `MyPromise`. Its return value becomes the fulfillment value of the promise returned by `then()`.
*
* It immediately returns an equivalent `MyPromise` object, allowing you to chain
* calls to other `MyPromise` methods. This new promise is always pending when returned, regardless of the current promise's status.
*
* @example
*
* then(onFulfilled)
* then(onFulfilled, onRejected)
* then(undefined, onRejected)
* then(undefined, undefined)
*
* then(
* (value) => { fulfillment handler },
* (reason) => { rejection handler },
* )
* @param {Function | Any} onFulfilled
* @param {Function | Any} onRejected
* @return {MyPromise}
*/
// 2.2
then(onFulfilled, onRejected) {
// 2.2.1.1, 2.2.7.3
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : (x) => x;
// 2.2.1.2, 2.2.7.4
onRejected = typeof onRejected === 'function' ? onRejected : (r) => {throw r};
// 2.2.7
return new MyPromise((resolve, reject) => {
if (this.#state === 'pending') {
// 2.2.2
this.#onFulfilledHandlers.push(() => {
this.#callCallback(this.#value, onFulfilled, resolve, reject);
});
// 2.2.3
this.#onRejectedHandlers.push(() => {
this.#callCallback(this.#value, onRejected, resolve, reject);
});
}
if (this.#state === 'fulfilled') {
// 2.2.4
setTimeout(() => {
this.#callCallback(this.#value, onFulfilled, resolve, reject);
}, 0);
}
if (this.#state === 'rejected') {
// 2.2.4
setTimeout(() => {
this.#callCallback(this.#value, onRejected, resolve, reject);
}, 0);
}
});
}
/**
* Syntactic sugar for .then(undefined, reject)
*
* @param {Function} onRejected
* @returns {MyPromise}
*/
catch(onRejected) {
return this.then(undefined, onRejected);
}
};