rampatel
rampatel

Reputation: 541

nodejs callback code is synchronized

I am learning nodejs prons and cons to make my new project. One doubts I still unclear i.e. code inside callback function in synchronized or thread safe ?

I knew that nodejs is working with single thread and its handovering execution in some other process inside callback function. However in case if first callback request would take time and second request come with different data then its creates havoc?

If its thread safe means every time its creating new thread inside callback function?

Upvotes: 0

Views: 109

Answers (1)

mscdex
mscdex

Reputation: 106736

You do not have to worry about thread safety and similar issues in your javascript code. All javascript is executed on the main thread. In fact, just about all operations in node happen on the same main thread (except a small number of tasks like DNS and file I/O which use the thread pool in libuv, but those all post back to the main thread via a queue which is read from when the main thread has reached the end of an event loop tick).

However, other issues such as race conditions could technically still occur in javascript, but from my experience that isn't too common.

Upvotes: 1

Related Questions