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

212
Views
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 answers
Answer question

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 Report

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 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!