2018-03-06 return next() 与 await next()

var http = require('http')
const Koa = require('koa')
const app = new Koa()

app.use(async(ctx,next)=>{
    console.log(1)
    await next()
    ctx.body = 'hello world0'
    console.log(2)
})
app.use(async(ctx,next)=>{
    console.log(3)
    ctx.body = 'hello world1'
    return next()
    console.log(4)
})
app.use(async(ctx,next)=>{
    console.log(5)
    ctx.body = 'hello world2'
    console.log(6)
})


http.createServer(app.callback()).listen(80, () => console.log(`listening on port 80`))
依次执行:
console.log( 1 )

console.log( 3 )
ctx.body = 'hello world1'

console.log( 5 )
ctx.body = 'hello world2'
console.log( 6 )

ctx.body = 'hello world0'
console.log( 2 )

所以最后浏览器输出:hello world0

你可能感兴趣的:(2018-03-06 return next() 与 await next())