美文网首页
2018-03-06 return next() 与 await

2018-03-06 return next() 与 await

作者: 路口师傅 | 来源:发表于2018-03-06 09:17 被阅读0次
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

      本文链接:https://www.haomeiwen.com/subject/tkhlfftx.html