手写Promise的实现

手写Promise的实现

Promise概述

  • Promise 就是一个类,在执行这个类的时候,需要传递一个执行器进去,执行器会立即执行
  • Promise 中有三种状态,分别为成功(fulfilled)、失败(rejected)、等待(pending),一旦状态确定就不可更改
  • resolve和rejected函数是用来更改状态的
  • then方法内部做的事情就是判断状态,如果状态是成功,调用成功的函数。如果状态是失败,调用失败的函数
  • then成功回调有一个参数,表示成功之后的值。then失败回调有一个参数,表示失败后的原因的值
1
2
3
4
5
let promise = new Promise((resolve, reject) => {
resolve('成功‘)
})

promise.then((res) => {}, (error) => {})

Promise简单实现

promse有三个状态,有resolve和reject更改状态,then接收成功和失败的值

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
const PENDING = 'pending' // 等待
const FULFILLED = 'fulfilled' //成功
const REJECTED = 'rejected' //失败

class MyPromise {
// executor 代表一个立即执行器,也就是回低调函数。constructor接收
constructor (executor) {
executor(this.resolve, this.reject)
}

// promise 状态
status = PENDING

// 成功之后的值
value = undefined

// 失败之后的原因
reason = undefined

// 箭头函数为了this指向类的实例对象,resolve把状态改成成功
resolve = value => {
// 因为promise的状态一旦确定无法更改,如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改成功
this.status = FULFILLED
// 保存成功之后的值
this.value = value
}

// reject把状态改成失败
reject = reason => {
// 如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改失败
this.status = REJECTED
// 保存失败之后的值
this.reason = reason
}

then (successCallback, failCallback) {
// 判断状态,执行回调函数,传人成功或失败的值
if (this.status === FULFILLED) {
successCallback(this.value)
} else if (this.status === REJECTED) {
failCallback(this.reason)
}
}
}

let promse = new MyPromise((resolve, reject) => {
// resolve('成功')
reject('失败')
})

promse.then(res => {
console.log(res)
}, error => {
console.log(error)
})

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
const PENDING = 'pending' // 等待
const FULFILLED = 'fulfilled' //成功
const REJECTED = 'rejected' //失败

class MyPromise {
// executor 代表一个立即执行器,也就是回低调函数。constructor接收
constructor (executor) {
executor(this.resolve, this.reject)
}

// promise 状态
status = PENDING

// 成功之后的值
value = undefined

// 失败之后的原因
reason = undefined

// 成功回调,因为异步resolve或reject,需要等待时间,此时的状态为等待中
successCallback = undefined

// 失败回调
failCallback = undefined

// 箭头函数为了this指向类的实例对象,resolve把状态改成成功
resolve = value => {
// 因为promise的状态一旦确定无法更改,如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改成功
this.status = FULFILLED
// 保存成功之后的值
this.value = value
// 判断成功回调是否存在,存在则调用,把值传入
this.successCallback && this.successCallback(this.value)
}

// reject把状态改成失败
reject = reason => {
// 如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改失败
this.status = REJECTED
// 保存失败之后的值
this.reason = reason
// 判断失败回调是否存在,存在则调用,把值传入
this.failCallback && this.failCallback(this.reason)
}

then (successCallback, failCallback) {
// 判断状态,执行回调函数,传人成功或失败的值
if (this.status === FULFILLED) {
successCallback(this.value)
} else if (this.status === REJECTED) {
failCallback(this.reason)
} else {
// 等待中,将成功回调和失败回调存储起来
this.successCallback = successCallback
this.failCallback = failCallback
}
}
}

let promse = new MyPromise((resolve, reject) => {
setTimeout(() => {
resolve('成功')
})
})

promse.then(res => {
console.log(res)
}, error => {
console.log(error)
})

then 方法多次调用添加多个处理函数.

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
const PENDING = 'pending' // 等待
const FULFILLED = 'fulfilled' //成功
const REJECTED = 'rejected' //失败

class MyPromise {
// executor 代表一个立即执行器,也就是回低调函数。constructor接收
constructor (executor) {
executor(this.resolve, this.reject)
}

// promise 状态
status = PENDING

// 成功之后的值
value = undefined

// 失败之后的原因
reason = undefined

// 成功回调,因为异步resolve或reject,需要等待时间,此时的状态为等待中,数组保存多个then的处理函数
successCallback = []

// 失败回调
failCallback = []

// 箭头函数为了this指向类的实例对象,resolve把状态改成成功
resolve = value => {
// 因为promise的状态一旦确定无法更改,如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改成功
this.status = FULFILLED
// 保存成功之后的值
this.value = value
// 判断成功回调是否存在,存在则调用,把值传入
// shift把前面的回调函数弹出来,每执行一个删除一个
while(this.successCallback.length) this.successCallback.shift()(this.value)
}

// reject把状态改成失败
reject = reason => {
// 如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改失败
this.status = REJECTED
// 保存失败之后的值
this.reason = reason
// 判断失败回调是否存在,存在则调用,把值传入
while(this.failCallback.length) this.failCallback.shift()(this.reason)
}

then (successCallback, failCallback) {
// 判断状态,执行回调函数,传人成功或失败的值
if (this.status === FULFILLED) {
successCallback(this.value)
} else if (this.status === REJECTED) {
failCallback(this.reason)
} else {
// 等待中,将成功回调和失败回调存储起来,调用添加多个处理函数 push进去
this.successCallback.push(successCallback)
this.failCallback.push(failCallback)
}
}
}

let promse = new MyPromise((resolve, reject) => {
setTimeout(() => {
resolve('成功')
})
})

promse.then(res => {
console.log(res)
}, error => {
console.log(error)
})
promse.then(res => {
console.log(res)
}, error => {
console.log(error)
})
promse.then(res => {
console.log(res)
}, error => {
console.log(error)
})

then 方法的链式调用

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
const PENDING = 'pending' // 等待
const FULFILLED = 'fulfilled' //成功
const REJECTED = 'rejected' //失败

class MyPromise {
// executor 代表一个立即执行器,也就是回低调函数。constructor接收
constructor (executor) {
executor(this.resolve, this.reject)
}

// promise 状态
status = PENDING

// 成功之后的值
value = undefined

// 失败之后的原因
reason = undefined

// 成功回调,因为异步resolve或reject,需要等待时间,此时的状态为等待中,数组保存多个then的处理函数
successCallback = []

// 失败回调
failCallback = []

// 箭头函数为了this指向类的实例对象,resolve把状态改成成功
resolve = value => {
// 因为promise的状态一旦确定无法更改,如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改成功
this.status = FULFILLED
// 保存成功之后的值
this.value = value
// 判断成功回调是否存在,存在则调用,把值传入
// shift把前面的回调函数弹出来,每执行一个删除一个
while(this.successCallback.length) this.successCallback.shift()(this.value)
}

// reject把状态改成失败
reject = reason => {
// 如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改失败
this.status = REJECTED
// 保存失败之后的值
this.reason = reason
// 判断失败回调是否存在,存在则调用,把值传入
while(this.failCallback.length) this.failCallback.shift()(this.reason)
}

then (successCallback, failCallback) {
// then链式调用,返回promise,并且把value传入下一个then
let promise2 = new MyPromise((resolve, reject) => {
// 判断状态,执行回调函数,传人成功或失败的值
if (this.status === FULFILLED) {
let x = successCallback(this.value)
// 判断x的值是普通值还是promise对象
// 如果是普通值,直接调用resolve
// 如果是promise对象,查看promise对象返回的结果
// 再根据promise对象返回的结果,决定调用resolove,还是调用reject
reslovePromise(x, resolve, reject)
} else if (this.status === REJECTED) {
failCallback(this.reason)
} else {
// 等待中,将成功回调和失败回调存储起来,调用添加多个处理函数 push进去
this.successCallback.push(successCallback)
this.failCallback.push(failCallback)
}
})
return promise2
}
}

function reslovePromise (x, resolve, reject) {
if (x instanceof MyPromise) {
// promise对象,x.then(value => resolve(value), reason => reject(reason))
x.then(resolve, reject)
} else {
// 普通值
resolve(x)
}
}

let promse = new MyPromise((resolve, reject) => {
resolve('成功')
})

promse.then(value => {
console.log(value)
return 18
}).then(value => {
console.log(value)
})

then 方法链式调用识别 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
const PENDING = 'pending' // 等待
const FULFILLED = 'fulfilled' //成功
const REJECTED = 'rejected' //失败

class MyPromise {
// executor 代表一个立即执行器,也就是回低调函数。constructor接收
constructor (executor) {
executor(this.resolve, this.reject)
}

// promise 状态
status = PENDING

// 成功之后的值
value = undefined

// 失败之后的原因
reason = undefined

// 成功回调,因为异步resolve或reject,需要等待时间,此时的状态为等待中,数组保存多个then的处理函数
successCallback = []

// 失败回调
failCallback = []

// 箭头函数为了this指向类的实例对象,resolve把状态改成成功
resolve = value => {
// 因为promise的状态一旦确定无法更改,如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改成功
this.status = FULFILLED
// 保存成功之后的值
this.value = value
// 判断成功回调是否存在,存在则调用,把值传入
// shift把前面的回调函数弹出来,每执行一个删除一个
while(this.successCallback.length) this.successCallback.shift()(this.value)
}

// reject把状态改成失败
reject = reason => {
// 如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改失败
this.status = REJECTED
// 保存失败之后的值
this.reason = reason
// 判断失败回调是否存在,存在则调用,把值传入
while(this.failCallback.length) this.failCallback.shift()(this.reason)
}

then (successCallback, failCallback) {
// then链式调用,返回promise,并且把value传入下一个then
let promise2 = new MyPromise((resolve, reject) => {
// 判断状态,执行回调函数,传人成功或失败的值
if (this.status === FULFILLED) {
// setTimeout 把里面代码变成异步代码,这样promise2才可以获取得到
setTimeout(() => {
// 判断x的值是普通值还是promise对象
// 如果是普通值,直接调用resolve
// 如果是promise对象,查看promise对象返回的结果
// 再根据promise对象返回的结果,决定调用resolove,还是调用reject
let x = successCallback(this.value)
reslovePromise(promise2, x, resolve, reject)
}, 0)
} else if (this.status === REJECTED) {
failCallback(this.reason)
} else {
// 等待中,将成功回调和失败回调存储起来,调用添加多个处理函数 push进去
this.successCallback.push(successCallback)
this.failCallback.push(failCallback)
}
})
return promise2
}
}

function reslovePromise (promise2, x, resolve, reject) {
// 自己返回自己
if (promise2 === x) {
return reject(new TypeError('自己返回自己'))
}
if (x instanceof MyPromise) {
// promise对象,x.then(value => resolve(value), reason => reject(reason))
x.then(resolve, reject)
} else {
// 普通值
resolve(x)
}
}

let promse = new MyPromise((resolve, reject) => {
resolve('成功')
})

let p1 = promse.then(value => {
console.log(value)
return p1
})
p1.then(value => {
console.log(value)
}, reason => {
console.log(reason)
})

将 then 方法的参数变成可选参数

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
const PENDING = 'pending' // 等待
const FULFILLED = 'fulfilled' //成功
const REJECTED = 'rejected' //失败

class MyPromise {
// executor 代表一个立即执行器,也就是回低调函数。constructor接收
constructor (executor) {
executor(this.resolve, this.reject)
}

// promise 状态
status = PENDING

// 成功之后的值
value = undefined

// 失败之后的原因
reason = undefined

// 成功回调,因为异步resolve或reject,需要等待时间,此时的状态为等待中,数组保存多个then的处理函数
successCallback = []

// 失败回调
failCallback = []

// 箭头函数为了this指向类的实例对象,resolve把状态改成成功
resolve = value => {
// 因为promise的状态一旦确定无法更改,如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改成功
this.status = FULFILLED
// 保存成功之后的值
this.value = value
// 判断成功回调是否存在,存在则调用,把值传入
// shift把前面的回调函数弹出来,每执行一个删除一个
while(this.successCallback.length) this.successCallback.shift()(this.value)
}

// reject把状态改成失败
reject = reason => {
// 如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改失败
this.status = REJECTED
// 保存失败之后的值
this.reason = reason
// 判断失败回调是否存在,存在则调用,把值传入
while(this.failCallback.length) this.failCallback.shift()(this.reason)
}

then (successCallback, failCallback) {
// 将 then 方法的参数变成可选参数
successCallback = successCallback ? successCallback : value => value;
failCallback = failCallback ? failCallback: reason => { throw reason };

// then链式调用,返回promise,并且把value传入下一个then
let promise2 = new MyPromise((resolve, reject) => {
// 判断状态,执行回调函数,传人成功或失败的值
if (this.status === FULFILLED) {
// setTimeout 把里面代码变成异步代码,这样promise2才可以获取得到
setTimeout(() => {
// 判断x的值是普通值还是promise对象
// 如果是普通值,直接调用resolve
// 如果是promise对象,查看promise对象返回的结果
// 再根据promise对象返回的结果,决定调用resolove,还是调用reject
let x = successCallback(this.value)
reslovePromise(promise2, x, resolve, reject)
}, 0)
} else if (this.status === REJECTED) {
failCallback(this.reason)
} else {
// 等待中,将成功回调和失败回调存储起来,调用添加多个处理函数 push进去
this.successCallback.push(successCallback)
this.failCallback.push(failCallback)
}
})
return promise2
}
}

function reslovePromise (promise2, x, resolve, reject) {
// 自己返回自己
if (promise2 === x) {
return reject(new TypeError('自己返回自己'))
}
if (x instanceof MyPromise) {
// promise对象,x.then(value => resolve(value), reason => reject(reason))
x.then(resolve, reject)
} else {
// 普通值
resolve(x)
}
}

let promse = new MyPromise((resolve, reject) => {
resolve('成功')
})
promse.then().then().then(value => console.log(value))

捕获错误及 then 链式

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
const PENDING = 'pending' // 等待
const FULFILLED = 'fulfilled' //成功
const REJECTED = 'rejected' //失败

class MyPromise {
// executor 代表一个立即执行器,也就是回低调函数。constructor接收
constructor (executor) {
// 捕捉错误
try {
executor(this.resolve, this.reject)
} catch (e) {
this.reject(e)
}
}

// promise 状态
status = PENDING

// 成功之后的值
value = undefined

// 失败之后的原因
reason = undefined

// 成功回调,因为异步resolve或reject,需要等待时间,此时的状态为等待中,数组保存多个then的处理函数
successCallback = []

// 失败回调
failCallback = []

// 箭头函数为了this指向类的实例对象,resolve把状态改成成功
resolve = value => {
// 因为promise的状态一旦确定无法更改,如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改成功
this.status = FULFILLED
// 保存成功之后的值
this.value = value
// 判断成功回调是否存在,存在则调用,把值传入
// shift把前面的回调函数弹出来,每执行一个删除一个
while(this.successCallback.length) this.successCallback.shift()(this.value)
}

// reject把状态改成失败
reject = reason => {
// 如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改失败
this.status = REJECTED
// 保存失败之后的值
this.reason = reason
// 判断失败回调是否存在,存在则调用,把值传入
while(this.failCallback.length) this.failCallback.shift()()
}

then (successCallback, failCallback) {
// then链式调用,返回promise,并且把value传入下一个then
let promise2 = new MyPromise((resolve, reject) => {
// 判断状态,执行回调函数,传人成功或失败的值
if (this.status === FULFILLED) {
// setTimeout 把里面代码变成异步代码,这样promise2才可以获取得到
setTimeout(() => {
try {
// 判断x的值是普通值还是promise对象
// 如果是普通值,直接调用resolve
// 如果是promise对象,查看promise对象返回的结果
// 再根据promise对象返回的结果,决定调用resolove,还是调用reject
let x = successCallback(this.value)
reslovePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
}, 0)
} else if (this.status === REJECTED) {
failCallback(this.reason)
} else {
// 等待中,将成功回调和失败回调存储起来,调用添加多个处理函数 push进去
this.successCallback.push(successCallback)
this.failCallback.push(failCallback)
}
})
return promise2
}
}

function reslovePromise (promise2, x, resolve, reject) {
// 自己返回自己
if (promise2 === x) {
return reject(new TypeError('自己返回自己'))
}
if (x instanceof MyPromise) {
// promise对象,x.then(value => resolve(value), reason => reject(reason))
x.then(resolve, reject)
} else {
// 普通值
resolve(x)
}
}

let promse = new MyPromise((resolve, reject) => {
// throw new Error('error')
resolve('成功')
})

promse.then(value => {
console.log(value)
throw new Error('then error')
}, reason => {
console.log(reason)
}).then(value => {
console.log(value)
}, reason => {
console.log('000')
console.log(reason.message)
})

Promise.all 方法的实现

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
const PENDING = 'pending' // 等待
const FULFILLED = 'fulfilled' //成功
const REJECTED = 'rejected' //失败

class MyPromise {
// executor 代表一个立即执行器,也就是回低调函数。constructor接收
constructor (executor) {
// 捕捉错误
try {
executor(this.resolve, this.reject)
} catch (e) {
this.reject(e)
}
}

// promise 状态
status = PENDING

// 成功之后的值
value = undefined

// 失败之后的原因
reason = undefined

// 成功回调,因为异步resolve或reject,需要等待时间,此时的状态为等待中,数组保存多个then的处理函数
successCallback = []

// 失败回调
failCallback = []

// 箭头函数为了this指向类的实例对象,resolve把状态改成成功
resolve = value => {
// 因为promise的状态一旦确定无法更改,如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改成功
this.status = FULFILLED
// 保存成功之后的值
this.value = value
// 判断成功回调是否存在,存在则调用,把值传入
// shift把前面的回调函数弹出来,每执行一个删除一个
while(this.successCallback.length) this.successCallback.shift()(this.value)
}

// reject把状态改成失败
reject = reason => {
// 如果状态不是等待,阻止程序向下执行
if (this.status !== PENDING) return
// 将状态更改失败
this.status = REJECTED
// 保存失败之后的值
this.reason = reason
// 判断失败回调是否存在,存在则调用,把值传入
while(this.failCallback.length) this.failCallback.shift()()
}

then (successCallback, failCallback) {
// 参数可选
successCallback = successCallback ? successCallback : value => value;
// 参数可选
failCallback = failCallback ? failCallback: reason => { throw reason };
// then链式调用,返回promise,并且把value传入下一个then
let promise2 = new MyPromise((resolve, reject) => {
// 判断状态,执行回调函数,传人成功或失败的值
if (this.status === FULFILLED) {
// setTimeout 把里面代码变成异步代码,这样promise2才可以获取得到
setTimeout(() => {
try {
// 判断x的值是普通值还是promise对象
// 如果是普通值,直接调用resolve
// 如果是promise对象,查看promise对象返回的结果
// 再根据promise对象返回的结果,决定调用resolove,还是调用reject
let x = successCallback(this.value)
reslovePromise(promise2, x, resolve, reject)
} catch (e) {
reject(e)
}
}, 0)
} else if (this.status === REJECTED) {
setTimeout(() => {
try {
let x = failCallback(this.reason);
// 判断 x 的值是普通值还是promise对象
// 如果是普通值 直接调用resolve
// 如果是promise对象 查看promsie对象返回的结果
// 再根据promise对象返回的结果 决定调用resolve 还是调用reject
resolvePromise(promsie2, x, resolve, reject)
}catch (e) {
reject(e);
}
}, 0)
} else {
// 等待
// 将成功回调和失败回调存储起来
this.successCallback.push(() => {
setTimeout(() => {
try {
let x = successCallback(this.value);
// 判断 x 的值是普通值还是promise对象
// 如果是普通值 直接调用resolve
// 如果是promise对象 查看promsie对象返回的结果
// 再根据promise对象返回的结果 决定调用resolve 还是调用reject
resolvePromise(promsie2, x, resolve, reject)
}catch (e) {
reject(e);
}
}, 0)
});
this.failCallback.push(() => {
setTimeout(() => {
try {
let x = failCallback(this.reason);
// 判断 x 的值是普通值还是promise对象
// 如果是普通值 直接调用resolve
// 如果是promise对象 查看promsie对象返回的结果
// 再根据promise对象返回的结果 决定调用resolve 还是调用reject
resolvePromise(promsie2, x, resolve, reject)
}catch (e) {
reject(e);
}
}, 0)
});
}
})
return promise2
}

// 不管 Promise 对象最后状态如何,都会执行的操作
finally (callback) {
return this.then(value => {
return MyPromise.resolve(callback()).then(() => value);
}, reason => {
return MyPromise.resolve(callback()).then(() => { throw reason })
})
}

// 发生错误时的回调函数
catch (failCallback) {
return this.then(undefined, failCallback)
}

static all (array) {
let result = []
let index = 0
return new MyPromise((resolve, reject) => {
function addData (key, value) {
result[key] = value
index++
// 等所有的异步操作执行完毕,返回所有结果
if (index === array.length) {
resolve(result)
}
}

for(let i = 0; i < array.length; i++) {
let current = array[i]
// 判断传入的是promise对象还是普通值
if (current instanceof MyPromise) {
// promise对象
current.then(value => addData(i, value), reason => reject(reason))
} else {
// 普通值
addData(i, array[i])
}
}
})
}

// 将现有对象转为 Promise 对象
static resolve (value) {
// 判断是否是 Promise 对象,是直接返回值,否则创建一个promise
if (value instanceof MyPromise) return value;
return new MyPromise(resolve => resolve(value));
}
}

function reslovePromise (promise2, x, resolve, reject) {
// 自己返回自己
if (promise2 === x) {
return reject(new TypeError('自己返回自己'))
}
if (x instanceof MyPromise) {
// promise对象,x.then(value => resolve(value), reason => reject(reason))
x.then(resolve, reject)
} else {
// 普通值
resolve(x)
}
}

function p1 () {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve('p1')
},2000)
})
}

function p2 () {
return new Promise(function (resolve, reject) {
resolve('p2')
})
}

MyPromise.all(['a', 'b', p1(), p2()]).then(result => {
console.log(result)
})