The content of this article is about the analysis of Event Loop in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
What is Event Loop?
Official website explanation
My personal understanding is that the single thread of js is that its task stack is single thread, but it handles asynchronous i/o The method is to rely on libuv to open the thread pool for processing. After completion, the task is added to the poll queue, and then when the task stack is empty or the event reaches the threshold, the poll queue and timer tasks are added to the task stack, and continue this Loop, this is generally the Event Loop of js.
Structure
┌───────────────────────────┐ ┌─>│ timers │ │ └─────────────┬─────────────┘ │ ┌─────────────┴─────────────┐ │ │ pending callbacks │ │ └─────────────┬─────────────┘ │ ┌─────────────┴─────────────┐ │ │ idle, prepare │ │ └─────────────┬─────────────┘ ┌───────────────┐ │ ┌─────────────┴─────────────┐ │ incoming: │ │ │ poll │<p><strong>timer:</strong><br> When the scheduled task reaches the threshold, it will not be executed immediately, but will be blocked by tasks waiting for the task stack. </p><p><strong>pending callbacks: </strong><br> This stage performs callbacks for certain system operations (such as TCP error types). For example, some *nix systems want to wait to report an error if a TCP socket is received while trying to connect. This will be queued for execution in the pending callback phase. </p><p><strong>pull:</strong> <br>This stage has two main functions: <br> Calculate how long it should block and poll for I/O, and then <br> Process the polling queue event. <br> When the event loop enters the polling phase and no timers are scheduled, one of two things will happen: </p><p> If the polling queue is not empty, the event loop will traverse it and execute them synchronously The queue is called back until the queue is exhausted, or a system-dependent hard limit is reached. </p><p>If the poll queue is empty, one of two things will happen: </p><p>If the script has executed setImmediate, the event loop will end the poll phase and continue with the check phase to execute these Scheduling script. </p><p>If the script does not execute setImmediate, the event loop will wait for the callback to be added to the poll queue and then execute it immediately. </p><p>Once the poll queue is empty the event loop will check the timer. If one or more timers are ready, the event loop will wrap around to the timer phase to execute the callbacks of those timers. </p><p><strong>check</strong> <br>This phase allows people to execute callbacks immediately after the polling phase is completed. If the polling phase becomes idle and there is a setImmediate task, the event loop jumps directly to check execution instead of blocking in the poll phase waiting for the callback to be added. </p><p>setImmediate is actually a special timer that runs in a separate stage of the event loop. It uses the libuv API to schedule callbacks to be executed after the polling phase is completed. </p><p><strong>close callbacks</strong></p><p>If the socket or handle is closed suddenly (such as socket.destroy()), the 'close' event will be emitted at this stage. Otherwise it will be emitted by process.nextTick(). </p><p>This article has ended here. For more other exciting content, you can pay attention to the <a href="http://www.php.cn/course/list/17.html" target="_blank">JavaScript Video Tutorial</a> column on the PHP Chinese website! </p><p><br></p><p> </p>
The above is the detailed content of Analysis of Event Loop in JavaScript. For more information, please follow other related articles on the PHP Chinese website!