there are many articles on browser event loops, including main threads, task queues, micro tasks, and macro tasks. It"s more or less the same! There is one thing I still don"t understand: how exactly is the browser implemented?
first! If an event occurs (whether it"s a network or mouse event.) this event needs to be added to the task queue to be processed, so the question is, how did he add the event to the queue? Or how did the browser know that something had happened?
my guess on this point is that maybe all asynchronous operations will call a function when there is a result (whether it is a function or a ghost, it is the same code anyway). The function of this code is to finally add the event handler to the task queue through various calculations.
then! How does the browser execute the loop mechanism? I have two guesses:
1, maybe the behavior of checking the queue in a loop is triggered by events. For example, when an asynchronous event arrives, the browser will not only call the code I mentioned above to place the event handler, but also trigger a queue check immediately after placement. If there is an event task, it will be taken out and executed immediately.
2, this is simple and violent. Maybe the browser will open another thread. There is a timer buried in this thread, just like setInterval, to check the task queue every 1 millisecond or even 0.5 millisecond. But this seems to be a waste of resources!
what do the bosses think?