Sudhanshu Gaur
Sudhanshu Gaur

Reputation: 7684

Can two callbacks execute the code at the same time(Parrallely) or not?

I am doing an IO wait operation inside a for loop now the thing is when all of the operations terminates I want to send the response to the server. Now I was just wondering that suppose two IO operation terminates exactly at the same time now can they execute code at the same time(parallel) or will they execute serially?

As far as I know, as Node is Concurrent but not the Parallel language so I don't think they will execute at the same time.

Upvotes: 0

Views: 1467

Answers (3)

jfriend00
jfriend00

Reputation: 707916

node.js runs Javascript with a single thread. That means that two pieces of Javascript can never be running at the exact same moment.

node.js processes I/O completion using an event queue. That means when an I/O operation completes, it places an event in the event queue and when that event gets to the front of the event queue and the JS interpreter has finished whatever else it was doing, then it will pull that event from the event queue and call the callback associated with it.

Because of this process, even if two I/O operations finish at basically the same moment, one of them will put its completion event into the event queue before the other (access to the event queue internally is likely controlled by a mutex so one will get the mutex before the other) and that one's completion callback will get into the event queue first and then called before the other. The two completion callbacks will not run at the exact same time.

Keep in mind that more than one piece of Javascript can be "in flight" or "in process" at the same time if it contains non-blocking I/O operations or other asynchronous operations. This is because when you "wait" for an asynchronous operation to complete in Javscript, you return control back to the system and you then resume processing only when your completion callback is called. While the JS interpreter is waiting for an asynchronous I/O operation to complete and the associated callback to be called, then other Javascript can run. But, there's still only one piece of Javascript actually ever running at a time.

As far as I know, as Node is Concurrent but not the Parallel language so I don't think they will execute at the same time.

Yes, that's correct. That's not exactly how I'd describe it since "concurrent" and "parallel" don't have strict technical definitions, but based on what I think you mean by them, that is correct.

Upvotes: 2

John
John

Reputation: 1704

Node.js is designed to be single thread. So basically there is no way that 'two IO operation terminates exactly at the same time' could happen. They will just finish one by one.

Upvotes: 1

Hamza Fatmi
Hamza Fatmi

Reputation: 1255

you can use Promise.all :

let promises = [];
for(...)
{  
  promises.push(somePromise); // somePromise represents your IO operation
}
Promise.all(promises).then((results) => { // here you send the response }  

You don't have to worry about the execution order.

Upvotes: 1

Related Questions