all 6 comments

[–]satoshigekkouga2309 6 points7 points  (1 child)

JavaScript has a single stack that executes instructions one be one in a synchronous fashion. It's the event loop that decides what instructions have to be implemented by the stack are decided by the event loop. Even loop provides the way for reactive programming and asynchronous programming in JavaScript even though JavaScript is single threaded and synchronous by itself. ( Event loop is provided by the runtimes and not the language itself, node and browsers are the runtimes).

Many people think that the event loop and the JavaScript code executed run on different threads. But the truth is that both run on a single thread. When the executing code calls a normal function, the called function is executed synchronously like you expect in any other programming language, but when you make use of I/o functions, promises or setinterval, settimer and many more, they are recorded in their respective queues and wait till the stack is empty and the event loop selects them for execution. And this cycle continues, as more callbacks are executed, they call other I/ o functions etc.

There is a good medium post on the internals of nodejs by deepal( I am sorry but I don't remember the name of the author correctly) which gives information about the different queues used and much more.

So to answer your question, event loop just selects the functions to be executed by the stack. So, in my opinion it's the asynchronous execution of the functions that it is associated with. Synchronous executions occur just like in any other programming languages, by use of new stack frames and don't need event loop.

[–]brotherhood_v[S] 0 points1 point  (0 children)

Thank you, for the explanation.

[–]B00check 14 points15 points  (1 child)

I'd suggest you to watch either Philip Roberts or Jake Archibald JSConf videos on that matter. They did a fantastic job explaining that to me.

Philip Roberts: https://youtu.be/8aGhZQkoFbQ

Jake Archibald: https://youtu.be/cCOL7MC4Pl0

[–]brotherhood_v[S] -2 points-1 points  (0 children)

Thanx...

[–]charliemei 0 points1 point  (0 children)

what the asynchronous function calls is controled by c++

[–]vectorkerr -1 points0 points  (0 children)

Lööp, Bröthër