To optimize the response delay, it is necessary to perform work after are response has been sent back to the client. However, the only way I can seem to get code to run after the response is sent is by using setTimeout
. Is there a better way? Perhaps somewhere to plug in code after the response is sent, or somewhere to run code asynchronously?
Here's some code.
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
callbacks for this kind of thing – Kalindiresponse.end()
to send/flush the response, and continue with your work. – Colaresponse.end()
isn't defined: github.com/koajs/koa/blob/master/docs/api/response.md. I usekoa-router
. I'll add some code. – Chevaldefriseresponse.end()
isnt supported by koa: github.com/koajs/koa/blob/master/docs/api/context.md#ctxres – ChevaldefrisesetImmediate()
. if it "looks" synchronous in koa, it's going to block the request. – Interdiction