手动实现 promise, 下面hi全部的代码,写了差不多有两个小时,并且附上示例

之前也有一版,不过我觉得这一版更好理解一些

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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// this code for create a new promise

const PROMISE_PADDING = "padding"
const PROMISE_FULLFILLED = "fullfilled"
const PROMISE_REJECTED = "rejected"

class newPromise {
constructor(func) {
this.status = PROMISE_PADDING
this.value = undefined
this.resolveCB = []
this.rejectCB = []

const resolve = (val) => {
if(this.status !== PROMISE_PADDING) {
return
}
this.value = val
this.status = PROMISE_FULLFILLED
this.resolveCB && this.resolveCB.map(fn => {
this.value = fn(this.value)
})
}

const reject = (val) => {
if(this.status !== PROMISE_PADDING) {
return
}
this.value = val
this.status = PROMISE_REJECTED
this.rejectCB && this.rejectCB.map(fn => {
this.value = fn(this.value)
})
}
try {
func(resolve, reject)
} catch (error) {
reject(error.message)
}
}

then(res, rej) {
res = typeof res !== "function" ? (val) => val : res
rej = typeof rej !== "function" ? (val) => val : rej

return new newPromise((resolve, reject) => {
if(this.status === PROMISE_PADDING) {
// 这里之所以用 个函数包围住,主要是因为多次使用 .then 的时候,后面的 then 拿到的是前一个then 返回的实例,所以会出现,后面的then,无法执行
// 使用函数包裹之后,然后调用 resolve 和reject 保证后续 then 方法能够拿到正确的值
this.rejectCB.push(() => {
try {
let val = rej(this.value)
reject(val)
} catch (error) {
reject(error.message)
}
})
this.resolveCB.push(() => {
try {
let val = res(this.value)
resolve(val)
} catch (error) {
reject(error.message)
}
})
}
if(this.status === PROMISE_FULLFILLED) {
try {
this.value = res(this.value)
resolve(this.value)
} catch (error) {
reject(error.message)
}
}
if(this.status === PROMISE_REJECTED) {
try {
this.value = rej(this.value)
reject(this.value)
} catch (error) {
reject(error.message)
}
}
})
}

catch(rej) {
return this.then(undefined, rej)
}

static resolve(fn) {
fn = typeof fn !== "function" ? (val) => val : fn
if(fn instanceof newPromise) {
return fn
}
return new newPromise((resolve, reject) => {
try {
this.value = fn()
resolve(this.value)
} catch (error) {
reject(error.message)
}
})
}

static reject(fn) {
fn = typeof fn !== "function" ? (val) => val : fn
if(fn instanceof newPromise) {
return fn
}
return new newPromise((resolve, reject) => {
try {
this.value = fn()
reject(this.value)
} catch (error) {
reject(error.message)
}
})
}

static all(...args) {
return new newPromise((resolve, reject) => {
let arr = []
if(args.length === 0) {
resolve([])
return
}
for(let i = 0; i < args.length; i++) {
let item = args[i]
if(!(item instanceof newPromise)) {
item = newPromise.resolve(item)
}
console.log(item instanceof newPromise)
item.then(res => {
arr[i] = {
status: PROMISE_FULLFILLED,
value: res
}
if(arr.every(item => item) && arr.length === args.length) {
resolve(arr)
}
}, rej => {
reject(rej)
})
}
})
}
static allSettled(...args) {
return new newPromise((resolve, reject) => {
let arr = []
if(args.length === 0) {
resolve([])
return
}
for(let i = 0; i < args.length; i++) {
let item = args[i]
if(!(item instanceof newPromise)) {
item = newPromise.resolve(item)
}
item.then(res => {
arr[i] = {
status: PROMISE_FULLFILLED,
value: res
}
if(arr.every(item => item) && arr.length === args.length) {
resolve(arr)
}
}, rej => {
arr[i] = {
status: PROMISE_REJECTED,
value: rej
}
if(arr.every(item => item) && arr.length === args.length) {
resolve(arr)
}
})
}
})
}
static race(...args) {
return new newPromise((resolve, reject) => {
let arr = []
if(args.length === 0) {
resolve([])
return
}
for(let i = 0; i < args.length; i++) {
let item = args[i]
if(!(item instanceof newPromise)) {
item = newPromise.resolve(item)
}
item.then(res => {
resolve(res)
}, rej => {
rej(arr)
})
}
})
}
static any(...args) {
return new newPromise((resolve, reject) => {
let arr = []
if(args.length === 0) {
resolve([])
return
}
for(let i = 0; i < args.length; i++) {
let item = args[i]
if(!(item instanceof newPromise)) {
item = newPromise.resolve(item)
}
item.then(res => {
resolve(res)
}, rej => {
arr[i] = {
status: PROMISE_FULLFILLED,
value: rej
}
if(arr.every(item => item) && arr.length === args.length) {
reject(arr)
}
})
}
})
}
}

// new newPromise((resolve, reject) => {
// setTimeout(() => {
// resolve(1000)
// }, 2000);
// }).then((res => {
// console.log(res, "res1")
// })).then(res => {
// console.log(res, "res4")
// })
// new newPromise((resolve, reject) => {
// // resolve(1000)
// throw new Error(12345)
// }).then(res => {
// console.log(res, "res2")
// return res
// }).then(res => {
// console.log(res, "res3")
// }).catch(err => {
// console.log(err, "err")
// })
// newPromise.resolve(() => {
// return 10000
// }).then(res => {
// console.log(res)
// })
// newPromise.all(() => {throw new Error(123)},() => 234, new newPromise((resolve) => setTimeout(() => resolve(1000), 1000))).then(res => {
// console.log(res)
// }, err => {
// console.log(err)
// })
// newPromise.allSettled(() => {throw new Error(123)},() => 234, new newPromise((resolve) => setTimeout(() => resolve(1000), 1000))).then(res => {
// console.log(res)
// }, err => {
// console.log(err)
// })
// newPromise.race(() => {throw new Error(123)},() => 234, new newPromise((resolve) => setTimeout(() => resolve(1000), 1000))).then(res => {
// console.log(res)
// }, err => {
// console.log(err)
// })
Promise.race([new Promise((resolve, reject) => {
setTimeout(() => reject(3234), 500)
}), new Promise((resolve) => {
setTimeout(() => resolve(1000), 1000)
})]).then(res => {
console.log(res)
}, err => {
console.log(err)
})
newPromise.any(() => {throw new Error(123)},() => 234, new newPromise((resolve) => setTimeout(() => resolve(1000), 1000))).then(res => {
console.log(res)
}, err => {
console.log(err)
})
// promise.then((res) => {
// console.log(res)
// }).then(res => {
// console.log(res, "res")
// throw new Error("1234")
// }).catch((err) => {
// console.log(err, "1234")
// })

// Promise.resolve(100).then((res) => {
// console.log(res, "promise")
// }).then(res => {
// console.log(res, "promise res")
// })