Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running code AFTER the response has been sent by Koa

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'
like image 739
Garrett Avatar asked Oct 22 '14 20:10

Garrett


1 Answers

Do NOT call ctx.res.end(), it is hacky and circumvents koa's response/middleware mechanism, which means you might aswell just use express. Here is the proper solution, which I also posted to https://github.com/koajs/koa/issues/474#issuecomment-153394277

app.use(function *(next) {
  // execute next middleware
  yield next
  // note that this promise is NOT yielded so it doesn't delay the response
  // this means this middleware will return before the async operation is finished
  // because of that, you also will not get a 500 if an error occurs, so better log it manually.
  db.queryAsync('INSERT INTO bodies (?)', ['body']).catch(console.log)
})
app.use(function *() {
  this.body = 'Hello World'
})

No need for ctx.end()
So in short, do

function *process(next) {
  yield next;
  processData(this.request.body);
}

NOT

function *process(next) {
  yield next;
  yield processData(this.request.body);
}
like image 99
felixfbecker Avatar answered Sep 28 '22 16:09

felixfbecker