Event loop in jsBecause JavaScript is single-threaded, the same event can only execute one method, so the methods in the program will be added to the execution stack and executed in order of last-in-first-out. When encountering an asynchronous task, it will not be blocked, but the task will be placed in the event queue and the synchronous code in the execution stack will continue to execute. When all tasks in the current execution stack are executed, the task in the event queue will be searched, and the task's callback function will be placed in the execution stack to execute the synchronous code in it. This repeated cycle is called an event loop. node.jsFeatures of Node.jsEvent-drivenExecute the code from top to bottom, and when a callback is required, add it to the event queue. When the main thread finishes running, execute the callback in the event queue. The whole process will not block new events, nor does it need to maintain already established events. Non-blocking ioWhen the main thread is idle, it starts to loop the event queue and process the events in the event queue. If the event is not an IO task, it will be processed by itself. If it is an IO task, it will be handed over to the thread pool for processing and a callback function will be specified. Then, other events in the loop queue will continue. When the blocking operation is completed, the result and the callback function will be placed in the queue, and the callback function will be executed when the main thread loops. Node.js advantages and disadvantagesadvantage
shortcoming:
Applicable scenarios:
Node.js event loopThe node event loop relies on the libuv engine. After v8 interprets the js code, it calls the corresponding node api. These apis are driven by the libuv engine to perform corresponding tasks and put different events into different queues waiting for the main thread to execute. Therefore, the node event loop exists in the libuv engine. libuv engine: implements event loop, file operations, etc., and is the core of node.js to achieve asynchronous The single thread of node.js only means that JavaScript runs in a single thread, and io operations can be completed through the thread pool internally poll (query phase) ---》check (check phase) ---》close callback (close event callback phase) ---》timer (timer detection phase) ---》io callback phase ---》idle phase ---》polling phase Poll phase (polling phase): V8 parses the js code and passes it to the libuv engine, and the loop first enters the poll stage. First check whether there is an event in the poll queue, and if there is, execute the callback in first-in-first-out order This is the end of this article about the JavaScript event loop case study. For more information about JavaScript event loop, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed steps to install xml extension in php under linux
>>: Basic usage and examples of yum (recommended)
Download Nginx image in Docker docker pull nginx ...
The Internet is an organism that is constantly ev...
This article example shares the specific code of ...
This article uses examples to describe how to bac...
1. Introduction CentOS8 system update, the new ve...
MySQL 5.5 installation and configuration method g...
Table of contents Preface What is data binding? T...
Preface Before we start explaining the principle ...
Copy code The code is as follows: <!DOCTYPE ht...
Table of contents What is front-end routing? How ...
Table of contents The relationship between the co...
Add an input file HTML control to the web page: &...
Table of contents 1. Introduction 2. Understand t...
This article mainly introduces the detailed proce...
Achieve resultsImplementation Code html <heade...