Skip to content

Async operation before send #158

Open
@eXist-FraGGer

Description

@eXist-FraGGer

Hello everyone!

I need to save some important data every time when the response is already available.

I have tried 2 approaches:

  1. api.finally - does not allow us to use an async callback.
api.finally(async (req, res) => {
  await saveAudit(req, res)
  res.cors()
  console.log('done')
})
  1. Tried to override the method res.send in a middleware - it looks like the send method can not include any async operations.
function auditHandler (handler) {
  return (req, res, next) => {
    res.sendRes= res.send
    res.send = function (body) {
      const data = {}
      saveAudit(data)
      .finally(() => {
        console.log('finally', resData)
        res.sendRes.call(this, body)
      })
    }
    next()
  }
}

As a result - the API does not work, but it works if we do not wait for an asynchronous operation.

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions