-
Notifications
You must be signed in to change notification settings - Fork 0
/
myPromise.js
271 lines (253 loc) · 7.64 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const { read } = require("fs-extra");
const PENDING = "PENDING";
const RESOLVED = "RESOLVED";
const REJECTED = "REJECTED";
class Promise {
constructor(executor) {
this.state = PENDING;
this.value = null;
this.reason = null;
this.onFulfilledCallbacks = [];
this.onRejectedCallbacks = [];
// 具有等待功能,衍生出了 Promise.resolve
const resolve = (value) => {
if (value instanceof Promise) {
//把resolve和reject分别当作onFulfilled和onRejected的回调
// 必须要return
// 这个then方法调用的时候,resolve已经有值了,
// 不用担心是先调用再赋值给resolve
return value.then(resolve, reject);
}
if (this.state === PENDING) {
this.state = RESOLVED;
this.value = value;
this.onFulfilledCallbacks.forEach((cb) => cb());
}
};
//没有等待功能,直接失败,衍生出了Promise.reject
const reject = (reason) => {
if (this.state === PENDING) {
this.state = REJECTED;
this.reason = reason;
this.onRejectedCallbacks.forEach((cb) => cb());
}
};
try {
executor(resolve, reject);
} catch (error) {
reject(error);
}
}
then(onFulfilled, onRejected) {
onFulfilled =
typeof onFulfilled === "function" ? onFulfilled : (value) => value;
onRejected =
typeof onRejected === "function"
? onRejected
: (err) => {
throw err;
};
const promise2 = new Promise((resolve, reject) => {
if (this.state === RESOLVED) {
setTimeout(() => {
try {
const x = onFulfilled(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0);
}
if (this.state === REJECTED) {
setTimeout(() => {
try {
const x = onRejected(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0);
}
if (this.state === PENDING) {
this.onFulfilledCallbacks.push(() => {
setTimeout(() => {
try {
const x = onFulfilled(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0);
});
this.onRejectedCallbacks.push(() => {
setTimeout(() => {
try {
const x = onRejected(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0);
});
}
});
return promise2;
}
//then的变种
catch(onRejected) {
return this.then(null, onRejected);
}
//具有等待性,就是去递归解析promise
//也是then的一个变种
//returns a new promise that is resolved when the original promise
//is resolved. The handler is called when the promise is settled,
//whether fulfilled or rejected.
//返回一个promise,当原promise resolve的时候,这个新的promise也会resolve。
//回调函数会在promise settle之后调用,不论是成功或者是失败
//onFinally的返回结果不会影响后续的promise
//只有一种情况,当onFinally返回promise的时候,如果成功不影响结果,失败才会影响结果
finally(onFinally) {
return this.then(
(data) => {
return Promise.resolve(onFinally()).then(() => data);
},
(err) => {
//上一次promise如果失败,resolve的时候也是先递归解析再返回
// return Promise.reject(callback()).then(null, () => {
// return err;
// });
return Promise.resolve(onFinally()).then(() => {
throw err;
});
}
);
}
//返回一个成功的promise,且具有等待功能
// Returns a new Promise object that is resolved with the given value.
// If the value is a thenable (i.e. has a then method),
// the returned promise will "follow" that thenable, adopting
// its eventual state; otherwise, the returned promise will be fulfilled
// with the value.
static resolve(value) {
//resolve,reject是构造器函数内定义好的
if (value instanceof Promise) {
return value;
}
return new Promise((resolve, reject) => {
if (
value &&
typeof value === "object" &&
typeof value.then === "function"
) {
() => {
value.then(() => {
console.log("内部then1");
resolve();
}, reject);
};
} else {
console.log("内部then2");
resolve(value);
}
});
}
static reject(reason) {
return new Promise((resolve, reject) => {
reject(reason);
});
}
// Promise .all -> Promise .allFulfilled
// Promise .allSettled -> Promise .allSettled
// Promise .race -> Promise .oneSettled
// Promise .any -> Promise .oneFulfilled
// 获取所有的成功结果,一旦有失败,就返回失败的结果
static all(promises) {
return new Promise((resolve, reject) => {
const result = [];
let count = 0;
promises = Array.from(promises);
//当传入空数组时,返回一个空数组
if (promises.length === 0) {
resolve(result);
}
for (let i = 0; i < promises.length; i++) {
const promise = promises[i];
promise
.then((data) => {
result[i] = data;
if (++count === promises.length) {
resolve(result);
}
})
.catch((err) => {
// reject并终止循环
return reject(err);
});
}
});
}
// 返回第一个完成的promise,不论是成功还是失败
static race(promises) {
return new Promise((resolve, reject) => {
promises = Array.from(promises);
for (let i = 0; i < promises.length; i++) {
const promise = promises[i];
promise
.then((data) => {
return resolve(data);
})
.catch((err) => {
return reject(err);
});
}
});
}
}
function resolvePromise(promise2, x, resolve, reject) {
if (promise2 === x) {
throw new TypeError("cycling detected");
}
if ((x && typeof x === "object") || typeof x === "function") {
let called = false;
try {
const then = x.then;
console.log("内部resolvePromise");
if (typeof then === "function") {
then.call(
x,
(r) => {
if (called) return;
called = true;
console.log("内部resolvePromise: onFullfiled");
resolvePromise(promise2, r, resolve, reject);
},
(err) => {
if (called) return;
called = true;
console.log("内部resolvePromise: onrejected");
reject(err);
}
);
} else {
//x.then是普通值
resolve(x);
}
} catch (error) {
if (called) return;
called = true;
reject(error);
}
} else {
//x是非promise值
resolve(x);
}
}
Promise.deferred = function () {
let dfd = {};
dfd.promise = new Promise((resolve, reject) => {
dfd.resolve = resolve;
dfd.reject = reject;
});
return dfd;
};
module.exports = Promise;