ES6 内置类 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
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
class Promise {
// promise 状态
PromiseState = 'pending'
// promise 结果
PromiseResult = null
// promise 回调函数列表
callback = []

constructor(executor) {
// 成功的回调
// 箭头函数避免 this 指向问题
const resolve = (data) => {
if (this.PromiseState !== 'pending') return
// 1. 修改 Promise 状态(promiseState)
// 2. 设置对象结果值(promiseResult)
this.PromiseState = 'fulfilled'
this.PromiseResult = data
// 异步调用 then 方法
setTimeout(() => {
// 调用成功的回调函数
this.callback.forEach(item => {
item.onResolved(data)
})
})
}

// 失败的回调
const reject = (data) => {
if (this.PromiseState !== 'pending') return
this.PromiseState = 'rejected'
this.PromiseResult = data
setTimeout(() => {
this.callback.forEach(item => {
item.onRejected(data)
})
})
}
try {
// 同步调用执行器函数
executor(resolve, reject)
} catch (e) {
// 修改 promise 对象状态为 失败
reject(e)
}
}

then(onResolved, onRejected) {
// onRejected 是否传递
if (typeof onRejected !== 'function') {
onRejected = (reason) => {
throw reason
}
}
// onResolved 是否传递
if (typeof onResolved !== 'function') {
onResolved = value => value
}

return new Promise((resolve, reject) => {
const callback = (type) => {
try {
let result = type(this.PromiseResult)
if (result instanceof Promise) {
// 是 Promise 类型
result.then(r => {
resolve(r)
}, e => {
reject(e)
})
} else {
resolve(result)
}
} catch (e) {
reject(e)
}
}


// 调用回调函数 PromiseState
if (this.PromiseState === 'fulfilled') {
// 回调函数异步执行
setTimeout(() => {
callback(onResolved)
})
}
if (this.PromiseState === 'rejected') {
setTimeout(() => {
callback(onRejected)
})
}

// pending 状态
if (this.PromiseState === 'pending') {
// 保存回调函数
this.callback.push({
onResolved: function () {
// 执行成功的回调函数
callback(onResolved)
},
onRejected: function () {
callback(onRejected)
}
})
}
})
}
catch(onRejected) {
return this.then(undefined, onRejected)
}

// 静态方法
static resolve(value) {
return new Promise((resolve, reject) => {
if (value instanceof Promise) {
value.then(v => {
resolve(v)
}, r => {
reject(r)
})
} else {
resolve(value)
}
})
}

static reject(value) {
return new Promise((resolve, reject) => {
reject(value)
})
}

static all(promises) {
return new Promise((resolve, reject) => {
let count = 0
const res = []
for (let i = 0; i < promises.length; i++) {
promises[i].then(v => {
count++
// 可能成功的时间不一致
// 使用push可能导致结果与传入数组不一致
res[i] = v
// 每个 promise 都成功
if (count === promises.length) {
resolve(res)
}
}, r => {
// 只要有一个失败
reject(r)
})
}
})
}

static race(promises) {
return new Promise((resolve, reject) => {
for (let i = 0; i < promises.length; i++) {
promises[i].then(v => {
resolve(v)
}, r => {
reject(r)
})
}
})
}

}