forked from MrTreasure/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
218 lines (200 loc) · 5.43 KB
/
index.ts
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
207
208
209
210
211
212
213
214
215
216
217
interface IExecutor {
(resolve: Function, reject: Function): void
}
enum STATUS {
PENDING,
FULFILLED,
REJECTED
}
class MyPromise {
private status: STATUS = STATUS.PENDING
private value: any
private reason: any
private onResolveCallbacks: Function[] = []
private onRejectCallbacks: Function[] = []
private executor: Function
private resolve: Function
private reject: Function
constructor (executor: IExecutor) {
let p = this
function resolve (value: any) {
setImmediate(() => {
if (p.status === STATUS.PENDING) {
p.status = STATUS.FULFILLED
p.value = value
p.onResolveCallbacks.forEach(fun => fun(value))
}
})
}
function reject (reason: any) {
setImmediate(() => {
if (p.status === STATUS.PENDING) {
p.status = STATUS.REJECTED
p.reason = reason
p.onRejectCallbacks.forEach(fun => fun(reason))
}
})
}
// this.executor = executor
// this.resolve = resolve
// this.reject = reject
executor(resolve, reject)
}
/**
* @method then 将异步执行完后的方法添加到 Promise 执行队列中
* @param {onFulfilled: Function} 异步执行成功时的函数
* @param {onReject: Function} 异步执行失败时的函数
*/
public then (onFulfilled: Function, onReject?: Function) {
// 包装一个Promise
let promise2: MyPromise
// 保存当前this,外部的Promise
const self: MyPromise = this
// 如果当前异步函数执行成功,得到了值
if (this.status === STATUS.FULFILLED) {
// 声明一个新的 promise2
promise2 = new MyPromise((resovle, reject) => {
setImmediate(() => {
try {
// then添加的方法返回一个promise
let res = onFulfilled(self.value)
resolvePromise(promise2, res, resovle, reject)
} catch (error) {
reject(error)
}
})
})
}
if (this.status === STATUS.REJECTED) {
promise2 = new MyPromise((resolve, reject) => {
setImmediate(() => {
try {
let res = onReject(self.reason)
resolvePromise(promise2, res, resolve, reject)
} catch (error) {
reject(error)
}
})
})
}
if (this.status === STATUS.PENDING) {
promise2 = new MyPromise((reslove, reject) => {
self.onResolveCallbacks.push(value => {
try {
let res = onFulfilled(value)
resolvePromise(promise2, res, reslove, reject)
} catch (error) {
reject(error)
}
})
self.onRejectCallbacks.push(reason => {
try {
let res = onReject(reason)
resolvePromise(promise2, res, reslove, reject)
} catch (error) {
reject(error)
}
})
})
}
return promise2
}
/**
* @description 和then方法的区别在于不再返回promise,一旦调用catch表示当前promise调用链的终结(原始的promise中没有这个限制,在未来的版本,catch之后还会条件finally事件)
* @param onReject 异步发生错误时执行的回调
*/
public catch (onReject) {
const self = this
const promise = new MyPromise((resolve, reject) => {
setImmediate(() => {
try {
let res = onReject(self.reason)
resolvePromise(promise, res, resolve, reject)
} catch (error) {
reject(error)
}
})
})
}
}
/**
* @function resolvePromise
* @param promise
* @param res
* @param resolve
* @param reject
*/
function resolvePromise (promise: MyPromise, res: any, resolve: Function, reject: Function) {
// 暂时不清楚什么情况下会出现循环应用
if (promise === res) {
return reject(new TypeError('循环引用'))
}
let then
let called
// 如果onFulfilled方法返回的res不为空,并且可能是object(可能是一个Promise或者一般对象)或者function
if (res !== null && ((typeof res === 'object' || typeof res === 'function'))) {
try {
then = res.then
// 如果res具有then属性,并且是一个function,说明可能是一个Promise(这里应该用类型判断)
if (typeof then === 'function') {
// 重复调用then方法, 直到res不再是一个Promise
then.call(res, function (res2) {
if (called) return
called = true
resolvePromise(promise, res2, resolve, reject)
}, function (err) {
if (called) return
called = true
reject(err)
})
} else {
// 调用resolve
resolve(res)
}
} catch (error) {
if (called) return
called = true
reject(error)
}
} else {
resolve(res)
}
}
// 测试代码
let p = new MyPromise((resolve, reject) => {
console.log(1)
setTimeout(() => {
console.log(2)
resolve(3)
}, 1000)
})
p.then((res) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(res)
resolve(4)
}, 1000)
})
}).then((res) => {
console.log(res)
})
p.then(res => {
console.log('p1:', res)
})
p.then(res => {
console.log('p1:', res)
})
let p2 = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('p2 1')
resolve(4)
})
})
p2.then((res) => {
console.log('p2:' + res)
})
setTimeout(() => {
p2.then((res) => {
console.log('p2:' + res)
})
}, 2000)