forked from FE-star/homework8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (39 loc) · 1.12 KB
/
index.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
function myPromise(constructor) {
let self = this;
self.status = "pending" //定义状态改变前的初始状态
self.value = undefined;//定义状态为resolved的时候的状态
self.reason = undefined;//定义状态为rejected的时候的状态
self.resolveCallback = undefined; // resolve回调
self.rejectCallback = undefined; // reject回调
function resolve(value) {
if(self.status === "pending"){
self.status = "fulfilled";
self.value = value
}
// TODO resolve如何改变状态及返回结果
}
function reject(reason) {
// TODO reject如何改变状态及返回结果
if(self.status === "pending"){
self.status = "rejected";
self.reason = reason
}
}
//捕获构造异常
try {
constructor(resolve, reject);
} catch (e) {
reject(e);
}
}
myPromise.prototype.then = function (onFullfilled, onRejected) {
//TODO then如何实现
if(this.status === "fulfilled"){
return onFullfilled(this.value)
}else if(this.status = "rejected"){
return onRejected(this.reason)
}else {
// setTimeout
}
}
module.exports = myPromise