自己实现一个Promise类

思考

最开始实现的时候总是把焦点放在函数上面,后面才发现应该将焦点放在所得到的结果,核心思想就是把resolve的值保存下来,然后执行then所传入的回调函数时,将值传入,reject同理。

对于异步的结果,当状态为pending时将onFulfilled函数保存,当函数执行resolve的时候再执行onFulfilled并把值传入。

实现

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
function Promise(executor) {
this.state = 'pending'
this.value = undefined
this.reason = undefined
this.onResolveAsyncCallbacks = []
this.onRejectAsyncCallbacks = []
const resolve = (value) => {
if (this.state === 'pending') {
this.state = 'fulfilled'
this.value = value // 保存结果
this.onResolveAsyncCallbacks.forEach(fn => fn(value)) // 执行异步回调
}
}
const reject = (reason) => {
if (this.state === 'pending') {
this.state = 'rejected'
this.reason = reason
this.onRejectAsyncCallbacks.forEach(fn => fn(reason))
}
}
try {
executor(resolve, reject)
} catch (err) {
reject(err)
}
}

Promise.prototype.then = function (onFulfilled) {
if (this.state === 'fulfilled') {
const result = onFulfilled(this.value)
// 如果结果是一个Promise对象则返回结果,实现链式调用
if (result && result.constructor === Promise) {
return result
}
}
if (this.state === 'pending') {
// this.onResolveAsyncCallbacks.push(onFulfilled)
// 返回Promise 避免异步返回 this,实现链式调用
return new Promise((resolve) => {
this.onResolveAsyncCallbacks.push(() => {
const result = onFulfilled(this.value)
if (isObject(result) && result.constructor.name === 'Promise') {
return result.then(r => resolve(r))
}
resolve(result)
})
})
}
return this
}

Promise.prototype.catch = function (onRejected) {
if (this.state === 'rejected') {
onRejected(this.reason)
}
if (this.state === 'pending') {
this.onRejectAsyncCallbacks.push(onRejected)
}
return this
}

Promise.resolve = function(value) {
return new Promise((resolve) => {
resolve(value)
})
}
Promise.reject = function(value) {
return new Promise((resolve, reject) => {
reject(value)
})
}
Partager