JavaScript implements the process of asynchronous programming

I believe many people expressed doubts about the JavaScript single thread: Why can I achieve asynchronous operation? In fact, JavaScript is indeed single-thread (we may wish to refer to the main thread), but it achieves the help of other threads of the browser in an asynchronous manner. How do other threads help the JavaScript main thread to achieve asynchronous? The answer is the task queue and event loop.

Task Series

First, as a single thread language, the tasks defined in JavaScript are executed in the main thread. But not every task will be executed immediately, and this task that does not immediately perform it is called asynchronous tasks. Instead, those who immediately implement them we call them synchronous tasks. These asynchronous tasks are handed over to other threads of the browser, but the main thread needs to understand the state of these asynchronous tasks, which is convenient for the next operation.

In a case, the main thread is preparing to cook, so I will reach an asynchronous task to buy vegetables. After the asynchronous task bought food, I have to tell the main thread: “I bought food”, this time the main thread I started cooking.

And we know that because JavaScript is a single thread, the above “Next Operation” cannot be directly defined in the main function (otherwise it is directly executed by the synchronization task), then these should be defined. Where is it? The answer is the callback function of the asynchronous task. In the JavaScript asynchronous mechanism, the task queue is the queue used to maintain the asynchronous task callback function. Such a queue is used to store these callback functions, which will wait until the main thread performs all synchronization functions, follow the advanced first mode. So after executing the task queue? Is the JavaScript main thread finished the execution? Of course, not, after the page is loaded, who will handle the interactive incidents of follow-up and users (such as clicking incident)?


We learn more about JavaScript asynchronous mechanisms through the above diagram..

Performs a synchronous task -> Check if there is a task in the task queue -> [If you do] -> Check if there is a task in the task queue -> [If you do] ->. ….

The main thread will check if there is a new “task” in the task queue in an unlimited job process after executing the synchronization task. If it is performed. These tasks include the callback function as defined in the asynchronous task, including the callback function of the user’s interactive event. Through the event cycle, JavaScript not only has a good task, but also has a good completion of the user’s interactive event. Because after completing the callback function of the asynchronous task, the tasks in the task queue are generated by the event, so we also call the above cycle process.

Console.log (‘Timer Go To Buy Cuisine’) Settimeout (Function () {Console .log (‘) is bought, the main thread goes to do the vegetable bar’)}, 0) console.log (‘You go to buy food first, I first see a World Cup’)
   In the browser, the above code is performed, and it is possible to better understand the asynchronous mechanism of JavaScript. 

In summary, the back of the JavaScript single thread has other threads of the browser to complete asynchronous services, these asynchronous tasks are communicated with the main thread, pass the callback The function is pushed to the task queue waiting for execution. The main thread is done is to perform the synchronization task, through the event loop, constantly check and execute the callback function in the task queue.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment