Node.js 关于koa2中的异步/等待
我在koa2中使用Node.js 关于koa2中的异步/等待,node.js,async-await,koa,ecmascript-2017,Node.js,Async Await,Koa,Ecmascript 2017,我在koa2中使用async/await,如下所示 const Koa = require('koa'); const app = new Koa(); app.use(async (ctx, next) => { await next() console.log("1------>"+Date.now()) }); app.use(async (ctx, next) => { await next() console.log("2------>"+D
async/await
,如下所示
const Koa = require('koa');
const app = new Koa();
app.use(async (ctx, next) => {
await next()
console.log("1------>"+Date.now())
});
app.use(async (ctx, next) => {
await next()
console.log("2------>"+Date.now())
});
app.use(async (ctx, next) => {
await next()
console.log("3------>"+Date.now())
});
app.use(async (ctx, next) => {
await next()
console.log("4------>"+Date.now())
});
app.listen(3000);
运行这个代码,我会得到这样的日志
4------>1526575713792
3------>1526575713794
2------>1526575713795
1------>1526575713795
下面是我的问题
app中的next
是什么。使用(async(ctx,next)
,它是函数吗wait next()
在每个应用程序中。使用函数意味着等待,请运行下一个应用程序。首先使用,如果没有下一个应用程序。使用,忽略它,然后在最后一个应用程序中运行代码。使用函数,对吗
app.use
更改为app.use(async(ctx)=>{console.log(“1----------->”+Date.now())});
,然后运行它,结果只有一条记录1----------->1526576945380
。我认为第二个app.use
函数将继续运行,我将得到这样的结果
4------>1526575713792
3------>1526575713794
2------>1526575713795
1------>1526575713795
1------->1526576945360
4----->1526575761869
3----->1526575761869
2------->1526575761869next
是一个函数,console.log(typeof next)
会向您显示这一点next
是一个函数,console.log(typeof next)
会向您显示这一点我明白了,非常感谢你,对于你的第三个答案,我没有期望,我认为所有的
应用程序。如果没有等待下一个,则将有序调用use
。我明白了,非常感谢你,对于你的第三个答案,我没有期望,我认为所有的应用程序。use
将有序调用如果没有等待下一步
,则选择此方式。