原生实现promise

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
272
273
274
275
276
277
278
/* ----------------------------------------------------------------------------
* Promise 完整版
--------------------------------------------------------------------------- */
const states = {
PENDING : 'pending',
FULFILLED: 'fulfilled',
REJECTED : 'rejected'
}

class Promise {
/**
* [ 构造函数 ]
* @param {[Function]} excurtor [执行函数]
* @return {[Undefined]} undefined
*/
constructor (excurtor) {
let self = this;

this.status = states.PENDING; // 当前状态
this.value = undefined; // 成功的内容
this.reason = undefined; // 失败的原因
this.onFulfilled = []; // 存储成功回调
this.onRejected = []; // 存储失败回调

// 成功方法
function resolve (value) {
let onFulfilled = self.onFulfilled;

// 判断状态是否为pending,只有此状态时,才可以发生状态改变
if (self.status === states.PENDING) {
self.status = states.FULFILLED;
self.value = value;

if (onFulfilled.length) {
onFulfilled.forEach(fn => {
fn();
});
}
}
}

// 失败方法
function reject (reason) {
let onRejected = self.onRejected;

// 判断状态是否为pending,只有此状态时,才可以发生状态改变
if (self.status === states.PENDING) {
self.status = states.REJECTED;
self.reason = reason;

if (onRejected.length) {
onRejected.forEach(fn => {
fn()
});
}
}
}

// 立即调用执行函数,并捕获异常
try {
excurtor(resolve, reject);
} catch (e) {
reject(e); // 捕获到执行函数异常后,直接执行失败回调
}
}

/**
* [ 实例方法: then ]
* @param {[Function]} onFulfilled [成功回调]
* @param {[Function]} onRejected [失败及异常回调]
* @return {[Object]} promise2
*/
then (onFulfilled, onRejected) {
// 补全参数,实现往下传递
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : value => value;
onRejected = typeof onRejected === 'function' ? onRejected : error =>{ throw error };

let self = this;
let promise2;

promise2 = new Promise((resolve, reject) => {
// 状态已改变为成功时,则立即执行
if (self.status === states.FULFILLED) {
setTimeout(() => { // 此定时器确保回调方法在异步中执行的,也方便进行promiseA+规范测试
// try catch 用来捕获回调方法中抛出的异常
try {
let x = onFulfilled(self.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
});
}

// 状态已改变为失败时,则立即执行
if (self.status === states.REJECTED) {
setTimeout(() => { // 同上
// 同上
try {
let x = onRejected(self.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
}

// 处理异步情况,先存在起来,等状态改变再执行相应回调
if (self.status === states.PENDING) {
self.onFulfilled.push(() => {
setTimeout(() => { // 同上
// 同上
try {
let x = onFulfilled(self.value);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
});
self.onRejected.push(() => {
setTimeout(() => { // 同上
// 同上
try {
let x = onRejected(self.reason);
resolvePromise(promise2, x, resolve, reject);
} catch (e) {
reject(e);
}
}, 0);
});
}
});

return promise2;
}

/**
* [ 实例方法:catch ]
* @param {[Function]} onRejected [失败及异常回调]
* @return {[Object]} promise
*/
catch (onRejected) {
return this.then(null, onRejected);
}

/**
* [ 静态方法(由类调用):all ]
* @param {[Array]} promises [由promise组成的数组]
* @return {[Object]} promise
*/
static all (promises) {
let res = [];
let count = 0;
let length = promises.length;

return new Promise((resolve, reject) => {
promises.forEach((promise, index) => {
promise.then(
value => {
res[index] = value;
if (++count === length) {
resolve(res);
}
},
reject
);
});
});
}

/**
* [ 静态方法(由类调用)race ]
* @param {[Array]} promises [由promise组成的数组]
* @return {[Object]} promise
*/
static race (promises) {
return new Promise((resolve, reject) => {
promises.forEach(promise => {
promise.then(resolve, reject);
})
});
}
}

function resolvePromise(promise2, x, resolve, reject) {
// 自己不能等待自己的状态改变
if (promise2 === x) {
return reject(new TypeError('循环引用'));
}

let called; // 标识位,要么成功,要么失败;
let typeX = typeof x;

// 判断x的类型是否为对象或者函数且不为null
if (x !== null && (typeX === 'object' || typeX === 'function')) {
try {
let then = x.then; // 获取then方法; 此处的try用于防止第三方库中获取值会抛错

if (typeof then === 'function') { // 说明返回的是promise事例
// 执行then方法,并将this指向x
then.call(x, y => {
if (called) return;
called = true;
// 如果返回的还是promise, 则将会继续递归调用
resolvePromise(promise2, y, resolve, reject);
}, e => {
if (called) return;
called = true;
reject(e);
});
} else { // x为变通对象时,直接成功
resolve(x);
}
} catch (e) {
if (called) return;
called = true;
reject(e);
}
} else {
resolve(x);
}
}

// 为了方便promiseA+规范测试
Promise.defer = Promise.deferred = function () {
let dtd = {};
dtd.promise = new Promise((resolve, reject) => {
dtd.resolve = resolve;
dtd.reject = reject;
});
return dtd;
};


module.exports = Promise;



// 测试例子 ---------------------------------------------------------------------
let promise = new Promise((resolve, reject) => {
resolve(1000);
//reject(100);
});

promise
.then(
value => {
console.log('then:', value);
return new Promise((resolve, reject) => {
//resolve(199);
reject('error123');
});
},
reason => console.log(`fail: ${ reason }`)
)
.then(
value => console.log(value),
reason => console.log(`fail: ${ reason }`)
);

Promise.all([
new Promise((resolve, reject) => {
resolve(1);
}),
new Promise((resolve, reject) => {
setTimeout(() => {
resolve(2);
}, 1000);
}),
new Promise((resolve, reject) => {
resolve(3);
//reject('出错了')
})
]).then(
value => console.log(value),
e => console.log(e)
);