-1

I know that Express allows to execute asynchronous functions in the routes and in the middlewares, but is this correct? I read the documentation and it specifies that NO ROUTES OR ASYNCHRONOUS MIDDLEWARES SHOULD BE ASSIGNED, today, currently, does Express support asynchronous functions? Does it block the execution process? o Currently asynchronous functions DO NOT BLOCK THE EXECUTION PROCESS?,

For example, if I place in an asynchronous route, and if requests are made in that route at the same time, are they resolved in parallel?, that is:

enter image description here

Or when assigning asynchronous routes, will these requests be resolved one after the other ?, that is:

enter image description here

This is what I mean by "blocking the execution process", because if one fails, are the other requests pending? or Am I misunderstanding?

I hope you can help me.

Grizzly
  • 371
  • 2
  • 13

1 Answers1

0

You can use async functions just fine with Express, but whether or not they block has nothing to do with whether they are async, but everything to do with what the code in the function does. If it starts an asynchronous operation and then returns, then it won't block. But, if it executes a bunch of time consuming synchronous code before it returns, that will block.

If getDBInfo() is asynchronous and returns a promise that resolves when it completes, then your examples will have the three database operations in flight at the same time. Whether or not they actually run truly in parallel depends entirely upon your database implementation, but the code you show here allows them to run in parallel if the database implements that.

The single thread of Javascript execution will run the first call to getDBInfo(), that DB request will be started and will immediately return a promise. Then, it will hit the await and it will suspend the execution of the containing function. That will allow the event loop to then start processing the second request and it will do the same. When it hits the await, it will suspend execution of the containing function and allow the event loop to process the third request will do likewise. Then, sometime later, one of the DB calls will complete (it could be any one of the three) which will resolve its promise which will unsuspend the function and it will send the response. Then, one after another the other two DB calls will finish and send their responses.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I update the content of the question with images and examples, I hope you can help me. – Grizzly Oct 15 '21 at 19:17
  • @Grizzly - More info added to my answer. – jfriend00 Oct 16 '21 at 06:20
  • Aa! now I understand, but to be clearer, when you say: "the function returns", you mean that what? send a response ?, that is, if I do this: `app.get('/', await (req, res) => { res.json ({ msg: 'ok' }) });`, The function "returns something" (returns an answer), is that what you mean?, since this would not block the process, right? – Grizzly Oct 17 '21 at 21:35
  • @Grizzly - Well, my answer doesn't say what you quoted: "the function returns" anywhere so I'm not really sure what you're asking about. This `app.get('/', await (req, res) => { ...});` would not be correct. You'd be trying to `await` a function reference which has no use. – jfriend00 Oct 17 '21 at 23:55
  • Sorry, apparently I'm wrong, I'm not fluent in English, maybe I'm wrong in translation, thanks anyway. – Grizzly Oct 26 '21 at 02:21