Promise
对Promise知识的一些文章整理,及其手写实现,加深自己的理解
# 我的手写Promise代码
Codesandbox 预览 (opens new window) stackblitz 预览 (opens new window)
Promise相关API
- Promise/A+
- then
- catch
- finally
- resolve
- reject
- all
- race
- any
- allSettled
# 手写Promise
- 完全理解并实现Promise(收藏) (opens new window)
- 一步一步手写完美符合PromiseA+规范的Promise (opens new window)
- 手写 Promise 全家桶 + Generator + async/await (opens new window)
- 这次彻底搞懂 Promise(手写源码多注释篇) (opens new window)
- Promise/async/Generator实现原理解析 (opens new window)
# Promise面试题
# 输出结果题
1. 输出执行结果
function f() {
setTimeout(() => {
console.log(5)
Promise.resolve().then(() => {
console.log(6)
})
})
new Promise((resolve,reject) => {
console.log(1)
resolve()
}).then(() => {
console.log(2)
Promise.resolve().then(() => {
console.log(3)
})
setTimeout(() => {
console.log(4)
})
Promise.resolve().then(() => {
console.log(7)
})
})
Promise.resolve().then(() => {
console.log(8)
})
}
1
2
8
3
7
5
6
4
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
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
编辑 (opens new window)
上次更新: 2021/10/19, 18:59:07