/
promise.js
171 lines (146 loc) · 4.81 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
/**
* Promise对象
* @param {Function} executor 执行器
*/
function Promise(executor) {
var _this = this;
this.state = 'pending';
this.value = undefined;
this.reason = undefined;
this.onFulfilledFunc = [];
this.onRejectedFunc = [];
try {
executor(resolve, reject);
} catch (e) {
reject(e.message);
}
function resolve(value) {
if (_this.state === 'pending') {
_this.value = value;
_this.state = 'resolved';
_this.onFulfilledFunc.forEach(f => f(value));
}
}
function reject(reason) {
if (_this.state === 'pending') {
_this.reason = reason;
_this.state = 'rejected';
_this.onRejectedFunc.forEach(f => f(reason));
}
}
}
/**
* 状态更新后执行
* @param {Function} onFulfilled 成功态回调
* @param {Function} onRejected 失败态回调
*/
Promise.prototype.then = function (onFulfilled, onRejected) {
//解决值的传递,如果没给任何参数就使用默认回调
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : (val) => val;
onRejected = typeof onRejected === 'function' ? onRejected : (e) => {
throw e
};
var promise2 = new Promise((resolve, reject) => {
if (this.state === 'pending') {
if (typeof onFulfilled === 'function') {
this.onFulfilledFunc.push((value) => {
setTimeout(() => {
try {
let x = onFulfilled(value);//要保证onFulfilled是异步执行
resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
});
});
}
if (typeof onRejected === 'function') {
this.onRejectedFunc.push((reason) => {
setTimeout(() => {
try {
let x = onRejected(reason);
resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
});
});
}
}
if (this.state === 'resolved') {
if (typeof onFulfilled === 'function') {
setTimeout(() => {
try {
let x = onFulfilled(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
});
}
}
if (this.state === 'rejected') {
if (typeof onRejected === 'function') {
setTimeout(() => {
try {
let x = onRejected(this.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
});
}
}
});
return promise2;
};
/**
* 根据上一个then的结果解析Promise
* @param {Object} promise2 新Promise对象
* @param {Object} x 上一个then的返回值
* @param {Function} resovle 新Promise的成功态回调
* @param {Function} reject 新Promise的失败态回调
*/
function resolvePromise(promise2, x, resolve, reject) {
if (promise2 === x) {
reject(new TypeError('Promise发生了循环引用'));
}
let called = false; //调用标记,防止成功和失败都调用
if (x !== null && (typeof x === 'object' || typeof x === 'function')) {
//是对象或函数
try {
let then = x.then;
if (typeof then === 'function') {
//如果是Promise就执行它,继续递归
then.call(x, (y) => {
if (called) return;
called = true; //调用过了,将标记更新
resolvePromise(promise2, y, resolve, reject);
}, (r) => {
if (called) return;
called = true; //调用过了,将标记更新
reject(r);
});
} else {
resolve(x);
}
} catch (error) {
if (called) return;
called = true; //调用过了,将标记更新
reject(error);
}
} else {
//是普通值
resolve(x)
}
}
//Code for test
// Promise.defer = Promise.deferred = function () {
// var dfd = {};
// dfd.promise = new Promise((resolve, reject) => {
// dfd.resolve = resolve;
// dfd.reject = reject;
// });
// return dfd;
// };
module.exports = Promise;