jeremydaly / jeremydaly/lambda-api

Async operation before send

Open
#158 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
1.5k
Forks
127
Avg merge
31m
Merged PRs (30d)
2

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names api.finally and a res.send override as the relevant response-lifecycle entry points. Start by tracing those paths in the framework and checking how middleware and response completion are tested. The issue names no files or tests and does not define an accepted behavior, so completion requires maintainer agreement on the async ordering.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.