洋葱模型
// await 将中间件函数切割了 只有next中的函数执行完毕之后,上一段中函数await后面的被入// 洋葱模型
/* async await *//* promise函数执行 1、then 2、在async函数中,通过await语句去执行*/在函数前面加上async 1.这个函数的返回值就是promise 2.可以在函数中使用await关键字
var a = new Promise((resolve,reject)=>{ setTimeout(()=>{ resolve(1) },2000)})var b = new Promise((resolve,reject)=>{ setTimeout(()=>{ resolve(2) },1000)})async function go(){ var res = await a; console.log(res); var sum = await b; console.log(sum);}go()
以鸡蛋 蛋壳 蛋白为例
app.use(async(ctx,next)=>{ console.log("蛋壳-->左"); await next() console.log("蛋壳-->右");app.use(async (ctx,next)=>{ console.log("蛋白-->左"); await next() console.log("蛋白-->右"); // console.log(3); // return "2"})app.use(async (ctx)=>{ console.log("蛋黄");}) 蛋壳-->左 蛋白-->左 蛋黄 蛋壳-->右 蛋白-->右