为了优化响应延迟,有必要在响应被发送回客户端之后执行工作。但是,在发送响应后,我似乎可以让代码运行的唯一方法是使用setTimeout
. 有没有更好的办法?也许在发送响应后插入代码的地方,或者异步运行代码的地方?
这是一些代码。
koa = require 'koa'
router = require 'koa-router'
app = koa()
# routing
app.use router app
app
.get '/mypath', (next) ->
# ...
console.log 'Sending response'
yield next
# send response???
console.log 'Do some more work that the response shouldn\'t wait for'