Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

310
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda