0

I have аn express route in which I call an async function. I don't want express wait till function be executed. Maybe it already works as I want, but I don't know how to check and prove it.

I have something like this:

app.get("/", function (req, res) {
  asyncFunctionCall();

  res.end();
});

In asyncFunctionCall I run slow HTTP-request. If main route logic will be executed before request, I don't want to wait it, but I want to return response to user.

Will Node.js wait until it ends or my http request will run in background without user-request?

  • Does this answer your question? [What happens to the promise if we don't await for it](https://stackoverflow.com/questions/61834275/what-happens-to-the-promise-if-we-dont-await-for-it) – shkaper May 10 '23 at 13:50
  • I realize your question is not _exactly_ about promises but most things still apply. Node.js will start executing your async function, then give control back to the request handler, which will go on to `res.end()`. The async operations will still run in background. The catch here is to be mindful of potential exceptions in the async function. – shkaper May 10 '23 at 13:54
  • Oh and I realized the title of your question does mention promises. – shkaper May 10 '23 at 13:55
  • I was afraid that Express perform async functions in another way (waiting till all started calls end while request in processing). Thanks for your explaining! – Андрей Смирнов May 10 '23 at 14:57
  • Also see https://stackoverflow.com/questions/32384449/can-i-fire-and-forget-a-promise-in-nodejs-es7 – Lin Du May 11 '23 at 04:00

0 Answers0