I use async/await
in koa2 like following
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);
Run this code, I will get the log like this
4------>1526575713792
3------>1526575713794
2------>1526575713795
1------>1526575713795
And here are my questions
What's the
next
inapp.use(async (ctx, next)
, is it a function?await next()
in eachapp.use
function meanswait, please run the next app.use first
, if has no nextapp.use
, ignore it, and run the code in lastapp.use
fuction, am I right?If I change the first one
app.use
toapp.use(async (ctx) => { console.log("1------>"+Date.now())});
, and run it, the result has only one records1------>1526576945380
. I thought the secondapp.use
function will continue running, and I will get the result like this1------>1526576945360
4------>1526575761869
3------>1526575761869
2------>1526575761869
So, can anyone help me? and explain for me? Many thanks.