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

214
Vistas
Can we return from an express call before a promise resolves?

Can we return from an express function call request before a promise within the function is completed ?

To be more precise, I have a route that when called will make an insert to the database. I am using typeorm as the ORM library to help this. Now, in order to make the application faster, I would like to return the request to the client even before the promise finishes.

I have provided a minimal example in the codeblock below.

app.get('/', (req, res) => {
  const user = new User();
  user.firstName = "Timber";
  user.lastName = "Saw";
  user.age = 25;

  // There is no await here. This is an asynchronous process.
  repository.save(user);
  res.send('Hello World!')
})

I know that this User object will get saved anyways. However I'm more worried regarding any potential problems with Node & Express if being done this way such as memory-leak?

Additional Information: If for some reason there is an error during saving, that is OK if the data is not written.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The async call will get put in the node event loop like any other call and will resolve when it can as you were able to observe in your application. There shouldn't be any concern with a memory leak as this is how asynchronous calls are meant to function.

The main draw backs are you are returning a success status code to the user without actually knowing there is a success and the eventual consistency the user will experience. If you are ok with those then there is no reason to not 'return' early.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use promise to send response (error handing part is referenced by https://expressjs.com/en/guide/error-handling.html)

example:

app.get('/', (req, res, next) => {
  const user = new User();
  user.firstName = "Timber";
  user.lastName = "Saw";
  user.age = 25;

  repository.save(user)
    .then((_) => {
      res.send('Hello World!')
    })
    .catch((err) => {
      next(err)
    })
})
about 4 years ago · Juan Pablo Isaza 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