forked from FE-star/homework8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (76 loc) · 2.53 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
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
function myPromise(executor) {
// 定义promise的状态
let self = this;
self.status = "pendding";
self.value = null; // 成功之后 返回数据
self.reason = null; // 失败之后返回原因
// 这个是 发布订阅
self.fulfilledCallbackList = [];
self.rejectCallbackList = [];
// 定义完成时的方法
function resolve(value) {
if (self.status == "pendding") {
self.status = "fulfilled";
self.value = value;
self.fulfilledCallbackList.forEach(item => item(value));
}
}
// 这个是 异常时候的方法
function reject(err) {
if (self.status == "pendding") {
self.status = "rejected";
self.reason = err;
self.rejectCallbackList.forEach(item => item(err));
}
}
// 立即执行 executor里面的方法
try {
executor(resolve, reject);
} catch (err) {
reject(err.message);
}
}
// 这个是 原型链上面的 then 方法
myPromise.prototype.then = function (onFulfilled, onRejected) {
// 这里 还要 判断一下 传入进来 是不是一个方法
onFulfilled = typeof onFulfilled == 'function' ? onFulfilled : function (data) { resolve(data) };
onRejected = typeof onRejected == 'function' ? onRejected : function (err) { throw err; }
let self = this;
// 这里是 then 里面的回调部分
if (self.status == "fulfilled") {
// 链式调用 就是 直接返回一个 Promise
return new myPromise((resolve, reject) => {
try {
let x = onFulfilled(self.value);
x instanceof myPromise ? x.then(resolve, reject) : resolve(x);
} catch (err) {
reject(err);
}
});
}
// 这个是 失败的状态
if (self.status == "rejected") {
return new myPromise((resolve, reject) => {
try {
let x = onRejected(self.reason);
x instanceof myPromise ? x.then(resolve, reject) : resolve(x);
} catch (err) {
reject(err);
}
});
}
// 这个是 pendding的状态
if (self.status == "pendding") {
return new myPromise((resolve, reject) => {
self.fulfilledCallbackList.push(() => {
let x = onFulfilled(self.value);
x instanceof myPromise ? x.then(resolve, reject) : resolve(x);
});
self.rejectCallbackList.push(() => {
let x = onRejected(self.reason);
x instanceof myPromise ? x.then(resolve, reject) : resolve(x);
});
});
}
}
module.exports = myPromise