The main thread of node executes js code. When it is an asynchronous operation, node will hand it over to libuv for execution, and then execute the code behind the asynchronous operation. After libuv is executed, it will be placed in a circular queue. libuv will have an event loop (Event Loop) similar to while(true). This event loop exists in the single thread of the node (that is, the main thread). The main thread loops Get the event execution callback function.
So what is the process of the main thread looping to retrieve events and executing the asynchronous code behind it? It is impossible for a thread to do two things at the same time. After executing the asynchronous code, will the CPU loop to fetch events when it is idle?
First of all, make it clear that the main thread will not poll when there is code execution.
So the main thread will execute all the initial code (non-callback code) before polling the event queue (the event queue can be regarded as a js object when being polled) because in theory the main thread of the js engine can only recognize js code.
But after the initial code is executed, the js main thread will poll for events. When a callback function is obtained, the js engine will not poll again until the current callback code is executed.
To summarize, only the js engine will poll when there is no code to execute. In other words, there is always only one callback function in the js main thread
I think this article can solve your doubts