Preface: js is a single-threaded language, so it is impossible for it to be asynchronous. However, the host environment of js (such as browser, node) is multi-threaded. The host environment makes js have asynchronous properties in some way (event-driven). In js, we generally divide all tasks into two categories, one is synchronous task and the other is asynchronous task. Among asynchronous tasks, there are more detailed classifications, namely microtasks and macrotasks. 1. Concept1.1 Macro Tasks Macro tasks---- In order to ensure that the JS internal tasks and 1.2 Microtasks Microtasks ---- Microtasks are usually tasks that need to be executed immediately after the current synchronous task is completed, such as providing feedback on a series of actions, or tasks that need to be executed asynchronously without allocating a new task, which can reduce performance overhead. 2. Execution OrderLet's look at a piece of code first, and then discuss the execution order: console.log(1) setTimeout(() => { console.log(2) }) Promise.resolve().then(() => { console.log(3) }) console.log(4) The result printed by the above code is 1 4 3 2. From the above code, we can conclude that their execution order is: Execute the synchronous code first. When encountering an asynchronous macro task, put the asynchronous macro task into the macro task queue. When encountering an asynchronous micro task, put the asynchronous micro task into the micro task list. When all the synchronous codes are executed, transfer the asynchronous micro task from the list to the main thread for execution. After the asynchronous micro task is executed, transfer the asynchronous macro task from the queue to the main thread for execution. The cycle continues until all tasks are executed.
3. Task Relationship Macro tasks are the mainstream. When Each macrotask can be followed by a microtask queue. If there are instructions or methods in the microtask queue, they will be executed first. If not, start executing the next macro task until all macro tasks are completed. 4. Task detailsWhy do microtasks still exist after macrotasks? That is because macrotasks take up too much performance. When some methods that were prepared earlier are needed and are executed last, and you don’t want to add a new macrotask, you can put these methods one by one in the microtask queue. After the code in this macrotask is executed, the microtask queue will be executed. Therefore, when the current synchronous code is executed and an asynchronous task is encountered, if it is an asynchronous macro task, it is placed in the next round of macro task queue; if it is an asynchronous micro task, it is placed in the micro task queue and follows the current macro task. A microtask is equivalent to the small tail of a macrotask. Therefore, when the current macrotask is executed, the asynchronous microtask waiting behind it will be immediately put into the queue for continued execution. The asynchronous macrotask needs to wait until the next round, which causes the asynchronous microtask to be executed before the macrotask. This is the end of this article about You may also be interested in:
|
<<: A brief discussion on the role of HTML empty links
>>: CSS realizes the realization of background image screen adaptation
Starting from MySQL 5.7, many security updates ha...
Unicode Signature BOM - What is the BOM? BOM is th...
1. Vertical table and horizontal table Vertical t...
1. Nested routing is also called sub-routing. In ...
background I am learning nodejs recently, and I r...
1. Install mutt sudo apt-get install mutt 2. Inst...
Table of contents How to represent the current ti...
Log in to your account export DOCKER_REGISTRY=reg...
3 ways to implement tab switching in Vue 1. v-sho...
Background: Make a little progress every day, acc...
Reasons why the 1px line becomes thicker When wor...
1. According to the online tutorial, the installa...
<br />The frame structure allows several web...
This article records the specific method of insta...
Table of contents 1. Props Parent >>> Ch...