-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08 promise.js
176 lines (164 loc) · 4.08 KB
/
08 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
// 定义状态
const PENDING = "pending";
const FULFILLED = "fulfilled";
const REJECTED = "rejected";
// myPromise
function myPromise(excutor) {
// 保存当前 promise 对象
let _this = this;
// 初始化状态
_this.status = PENDING;
// 用于存储 fulfilled 时的值
_this.value = undefined;
// 用于存储 rejected 时的值
_this.reason = undefined;
// 用于存储 fulfilled 状态对应的 onFulfilled 函数
_this.onFulfilledCallbacks = [];
// 用于存储 rejected 状态对应的 onRejected 函数
_this.onRejectedCallbacks = [];
/**
* resolve
* @param {any} value
*/
function resolve(value) {
if (value instanceof myPromise) {
return value.then(resolve, reject);
}
// 确保异步执行
setTimeout(() => {
if (_this.status === PENDING) {
_this.status = FULFILLED;
_this.value = value;
_this.onFulfilledCallbacks.forEach(cb => cb(_this.value));
}
});
}
/**
* reject
* @param {any} reason
*/
function reject(reason) {
setTimeout(() => {
if (_this.status === PENDING) {
_this.status = REJECTED;
_this.reason = reason;
_this.onRejectedCallbacks.forEach(cb => cb(_this.reason));
}
});
}
// 捕获 excutor 执行器中的异常
try {
excutor(resolve, reject);
} catch (err) {
reject(err);
}
}
/**
* 解析 then 返回值与新 Promise 对象
* @param {Object} newPromise 新的 Promise 对象
* @param {*} value 上一个 then 的返回值
* @param {Function} resolve newPromise 的 resolve
* @param {Function} reject newPromise 的 reject
*/
function resolvePromise(newPromise, value, resolve, reject) {
// 是否为循环调用
if (newPromise === value) {
reject(new TypeError("Promise 发生了循环调用."));
}
// value 为 Promise
if (
value !== null &&
(typeof value === "object" || typeof value === "function")
) {
// 对象或者函数
try {
let then = value.then; //取出then方法引用
if (typeof then === "function") {
then.call(
value,
_value => {
resolvePromise(newPromise, _value, resolve, reject);
},
err => {
reject(err);
}
);
} else {
resolve(value);
}
} catch (e) {
reject(e);
}
} else {
// 普通值
resolve(value);
}
}
// 在原型上定义 then 方法
myPromise.prototype.then = function(onFulfilled, onRejected) {
let _this = this;
let newPromise;
onFulfilled =
typeof onFulfilled === "function" ? onFulfilled : value => value;
onRejected =
typeof onRejected === "function"
? onRejected
: reason => {
throw reason;
};
// 成功态
if (_this.status === FULFILLED) {
newPromise = new myPromise((resolve, reject) => {
setTimeout(() => {
try {
let _value = onFulfilled(_this.value);
resolvePromise(newPromise, _value, resolve, reject);
} catch (err) {
reject(err);
}
});
});
}
// 失败态
if (_this.status === REJECTED) {
newPromise = new myPromise((resolve, reject) => {
setTimeout(() => {
try {
let _reason = onRejected(_this.reason);
resolvePromise(newPromise, _reason, resolve, reject);
} catch (err) {
reject(err);
}
});
});
}
// 等待态
if (_this.status === PENDING) {
newPromise = new myPromise((resolve, reject) => {
_this.onFulfilledCallbacks.push(value => {
try {
let _value = onFulfilled(value);
resolvePromise(newPromise, _value, resolve, reject);
} catch (err) {
reject(err);
}
});
_this.onRejectedCallbacks.push(reason => {
try {
let _reason = onRejected(reason);
resolvePromise(newPromise, _reason, resolve, reject);
} catch (err) {
reject(err);
}
});
});
}
return newPromise;
};
var a = new myPromise((resolve, reject) => {
resolve(123);
});
// console.log(a)
a.then(res => {
console.log(res);
});