【发布时间】:2018-08-02 19:32:27
【问题描述】:
为了优化响应延迟,有必要在响应发送回客户端后执行工作。但是,在发送响应后,我似乎可以让代码运行的唯一方法是使用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'
【问题讨论】:
-
如何发送请求?这种事情通常有
success回调 -
请向我们展示您的代码。通常,您只需致电
response.end()发送/刷新响应,然后继续您的工作。 -
@Bergi
response.end()未定义:github.com/koajs/koa/blob/master/docs/api/response.md。我使用koa-router。我会添加一些代码。 -
谢谢。但那不是 Coffeescript,不是 ES6 吗?
-
ES6 中的咖啡脚本。我只是忘了屈服。会修复。另外,koa 不支持
response.end():github.com/koajs/koa/blob/master/docs/api/context.md#ctxres
标签: javascript optimization generator ecmascript-6 koa