Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

308
Views
Calling async functions before ending HTTP Request in express

I have following route on my express server:

router.post("/doHeavyTask", async function (req, res) {

       
       const status = heavyTask(req)
       
       logAction(HEAVY_TASK) // logAction is defined below                  

       return res.status(status).send()


})

and the following log function that simply inserts a document to my mongoDB Database:

export function logAction(type: USER_LOG_TYPES){

  const db = getAppDatabase()
  const userLog: UserLog = {
    logType: type
  }
      
  getAppDatabase().db.collection('UserLogs').insertOne(userLog).then((insertedDoc) =>   {

      // ...
      // handle success
      return

    }).catch((error) => {

      // ... 
      // handle failure
      return
    
   })
}

Will calling an async function (in my case logAction) before ending a HTTP request cause any problems? Is there anything one should keep in mind?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Really depends on what your HEAVY TASK does. If it involves changing the user state a little, then after the time you complete res.send(), the user may have an alternate state.

E.x. if HEAVY TASK was to just add a log into the database, you can do that and tell the user it's being inserted by res.send(), and theoretically be fine.

But if TEAVY TASK wants to do something like changing username, then when you hit res.send(), the task will still be running and users for an instant still won't see their username changed.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!