-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyPromise.js
87 lines (71 loc) · 1.9 KB
/
MyPromise.js
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
/**
* 自己写一个符合 Promise/A+ 规范的 Promise
*/
// 声明 Promise 三种状态
const PENDING = "pending"
const RESOLVED = "resolved"
const REJECTED = "rejected"
// Promise 构造函数接收一个函数作为参数
function _Promise(fn) {
// 记录初始状态
var self = this
// 初始化状态
this.state = PENDING
// 记录状态变更后的值,即 resolved 或 rejected 传入值
this.value = null
// 保存 resolved 回调
this.resolvedCallbacks = []
// 保存 rejected 回调
this.rejectedCallbacks = []
// 状态变更为 resolved 时执行的函数
function resolve(value) {
// 保证代码的执行顺序为本轮事件循环的末尾
setTimeout(() => {
// 状态不可逆
if(self.state === PENDING) {
this.value = value
// 改变状态
self.state = RESOLVED
self.resolvedCallbacks.forEach(callback => {
callback(value)
})
}
}, 0)
}
// 状态变更为 rejected 时执行的函数
function reject(value) {
// 保证代码的执行顺序为本轮事件循环的末尾
setTimeout(() => {
// 状态不可逆
if(self.state === PENDING) {
this.value = value
// 改变状态
self.state = REJECTED
self.rejectedCallbacks.forEach(callback => {
callback(value)
})
}
}, 0)
}
try {
fn(resolve, reject)
} catch(e) {
// 若执行过程中报错,则执行 reject 函数
reject(e)
}
}
// 定义 then 方法,其接收两个函数作为参数
_Promise.prototype.then = function(onResolved, onRejected) {
if(this.state === PENDING) {
this.resolvedCallbacks.push(onResolved)
this.rejectedCallbacks.push(onRejected)
}
if(this.state === RESOLVED) {
onResolved(this.value)
}
if(this.state === REJECTED) {
onRejected(this.value)
}
}
_Promise.prototype.catch = function(reject) {
}