The answer is no. JavaScript engine is synchronous so it executes one line of code at a time.

So from where comes the Asynchronous calls ?!!
The Answer is JavaScript engine itself is synchronous but it interacts with other components in the browser like UI engine and Http Request through events like UI click event or HttpRequest got response event.
so All this events may be linked by call back functions and those functions executed when the event fire.

the next question is how those call back functions will be executed at the same time with normal JavaScript code running since JavaScript is synchronous ?!!
Actually there is something called event queue where all fired event callbacks wait here until JavaScript engine finishes any running code and when it finish it start looking at the event queue periodically.

do you mean that if a code still running and an event fired it will not be executed until the current code finished ?!!
yes that actually what will happen exactly and you can test this your self by this peace of code.this code execute while loop for three seconds and during this period you can fire click event and it will wait until loop to be finished and will wait the code after the loop to be finished then it will fire the event callback function.

reference : udemy


Comments are closed.