-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
46 lines (43 loc) · 1.11 KB
/
test.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
// koa2 中间件原理简析
// 中间件的仓库
const arr = [
async (next) => {
console.log(1);
await next();
console.log(2);
},
async (next) => {
console.log(3);
await new Promise((resolve, reject) => {
setTimeout(() => {
resolve(
console.log(4)
);
}, 3000);
}); // 异步操作 await 会等待后面的promise resolve 后再向下执行
await next();
console.log(5);
},
async (next) => {
console.log(6);
},
async (next) => {
// 不会执行 因为上一个函数中没有执行next
console.log(7);
await next();
console.log(8);
},
async (next) => {
// 不会执行 因为前面的函数中没有执行next
console.log(9);
}
];
function fun(arr) {
function dispose(index) {
const currentFun = arr[index];
const next = dispose.bind(null, index + 1);
return currentFun(next); // 尾递归
}
dispose(0);
}
fun(arr); // 先打印 1 3 一秒后打印4 6 5 2