造了套自己的 Promise

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
class PromiseT {
constructor() {
this.status = 'running';
this.val = null;
this.pendingList = [];
}
then(...list) {
this.pendingList.push(...list);
return this.run();
}
resolve(val) {
this.status = 'running';
if (val !== undefined) {
this.val = val;
}
return this.run();
}
pending() {
this.status = 'pending';
}
run() {
if (this.status === 'pending') {
return this;
}
const cal = this.pendingList.shift();
if (cal === undefined) {
return this;
}
const result = cal(this.val, this);
if (result === undefined) {
return this.run();
}
if (result.constructor === PromiseT) {
return result.then(...this.pendingList);
}
this.val = result;
return this.run();
}
}

测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
new PromiseT()
.resolve(5)
.then(x => x + 1)
.then(x => new PromiseT().resolve(2).then(a => x / a))
.then(x => console.log(x))
.then((x, p) => {
p.pending();
setTimeout(() => {
p.resolve(x - 3);
}, 2000);
})
.then(x => console.log(x));

// 输出 3
// 等待 2s
// 输出 0

毕竟是小轮子,catch() finally() 这些没写。。。