Does nodejs execute this function in parallel every time an event arrive, or nodejs will wait the execution to process the next event?
If this code receives rapid fire incoming events, it will not wait the execution for the first one to finish before the next one starts. As soon as the first one hits the await
or returns and there are events in the queue waiting to be processed, then next event will fire.
await
does not block the interpreter from going back to the event loop and processing more events. So, as soon as your code hits an await
, the interpreter will suspend execution of that function, return a promise from it (that's what all async
functions do) and as soon as the calling code returns, it will go back to the event loop to see if there are more incoming events to process. If so, it grabs the next one from the event queue and starts processing it.
This will result in all your callHttp(data)
functions running in parallel, all in flight at the same time.
This is an example of I/O and await
in Javascript are both non-blocking. The interpreter is free to process other incoming events during an asynchronous I/O call or during an await
.
If I change the await in a promise with .then, does it change something?
No. It changes nothing here.
If you needed to serialize the processing of these events such that you don't process event2
until event1
is done, you'd probably have to implement your own queue so that when event2
arrives, you check to see if you're already processing another event and, if so, you just insert event2
into your queue. Then, when any event finishes it's process, it notifies the queue so the queue can run the next event in line. But, at that point, I'd suggest a more top-down review of the overal design of what you're' trying to accomplish and how it is currently implemented because something is probably less-than-ideal in the design if you're forcing your server to serialize all these actions.