I still believe Javascript is single threaded, but when I think about the event handling mechanism, I have some doubts.
Why I think like this is even while processing one event from queue, it can listen or it can push events to same queue. I created an example as below:
<html>
<head>
<script>
function clicked(){
alert("clicked in between..");
}
function longRun(){
for(var i=0;i<50000;i++){
console.log(i);
}
alert("completed .... ");
}
</script>
</head>
<body>
<input type="button" value="quick!" onclick="clicked();"/>
<input type="button" value="long run!" onclick="longRun();"/>
</body>
</html>
When I click on long run! it will take some time to complete, but in the meanwhile if I click on quick! it will be added to the event queue and will be executed immediately after the long run event.
What is actually happening? Can anyone explain / correct me
Yes, the event loop runs on the same thread as your main function. No, the event loop doesn't start processing messages until your main function returns. console.
Event Loop uses Single Thread only. It is main heart of Node JS Platform Processing Model.
The Event Loop executes internal activities on any callbacks during this phase. Technically, there is no way to change the length or nature of this phase. During this phase, there is no mechanism in place to guarantee code execution.
Event Loop — Means single threaded infinite cycle which is making one task at a time and it's not only making single task queue, but it is also prioritizing tasks, because with event loop you have only one resource for execution (1 thread) so for executing some tasks right away you need prioritizing tasks.
Except for webWorkers (which we aren't talking about here), there is only one "user thread" per window in a browser. That means there's only one thread running your user Javascript.
This does not mean that the browser engine under the covers does not have some other threads to handle non-Javascript processing. In fact, this is very likely the case. But, since these other possible threads, never actually run any of your Javascript or affect any of your Javascript variables, they don't directly affect the JS execution environment.
Yes, these other threads may indeed insert things into the JS event queue that will be picked up later when the main JS thread is ready to process the next event.
See this answer How does JavaScript handle AJAX responses in the background? for more info and a list of related articles.
There is only one thread per tab. This thread does both JavaScript execution and screen updates. While longRun
is executing, you cannot react to a click on a button; the event you registered will be fired after all 50000 iterations are complete.
To clarify: events get put on a queue. The queue is getting executed in order by the tab thread.
EDIT: And, indeed, there's workers, but they act as if they were executing in a different tab - they have their own context, and can only communicate with the invoking program using postMessage
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With