当前位置:网站首页>Micro task, macro task and event loop of JS
Micro task, macro task and event loop of JS
2022-06-12 12:25:00 【I'm Mr.C】
JS Asynchronous is divided into micro task and macro task
To understand micro and macro tasks, you need to know JS The order in which the code runs .
JS A single thread , That is, things can only be done one by one .
JS The task queue is executed after the main thread's task is executed .
Task queue : stay JS Runtime , Execute the synchronized code first , Asynchronous code will be hung in the task queue , The asynchronous code of the task queue will not be executed until the main thread is completed .
console.log(1);
console.log(2);
setTimeout(()=>{
console.log(3);
},0);
console.log(4);
First understand the execution sequence of the above code :
First execute the code of the main thread , So first output 1,2 encounter setTimeout when , because setTimeout Is asynchronous , So it will be hung in the task queue first , Then continue the execution of the main thread , Then the output 4, Main thread completed ,JS Will execute the code of the task queue , So the final output 3.
The output order is 1,2,4,3
Asynchrony can be divided into micro tasks and macro tasks .
Common macro tasks :setTimeout、setInterval、DOM event 、setImmediate
Common micro tasks :Promise、process.nextTick
above all —— Micro task takes precedence over macro task
console.log(1);
console.log(2);
setTimeout(()=>{
console.log(3);
},0);
console.log(4);
Promise.resolve().then(()=>{
console.log(5);
}
By analyzing the above code first, you will have a deeper understanding of micro tasks and macro tasks ;
JS First execute the code of the main thread , So first output 1,2 encounter setTimeout 了 , Just put setTimeout Hang in the task queue , Continue to execute the code of the main thread , Output 4, encounter Promise 了 , Also put Promise Hang in the task queue ; So for now, output 1,2,4; In the task queue ,Promise It's a micro task , Takes precedence over macro tasks , therefore Promise Execute first , Output 5,setTimeout Finally, execute , Output 3.
In sequence :1,2,4,5,3.
Event Loop Also called JS Event cycle mechanism ;
We already know JS In addition to the main thread, the code also has a task queue ;Event Loop This means that the main thread has finished executing ,JS You will get the task in the task queue , After the task is completed, it will return to the task queue to get the task again , It just keeps cycling .
边栏推荐
- 树的前序,中序,后序遍历
- MVC模式、加密、jsonwebtoken
- Bat interview & advanced, get interview materials at the end of the text
- Basic concepts of machine learning
- MVC mode, encryption, jsonwebtoken
- Find the median of two ordered arrays (leetcode 4)
- Click to generate 4-bit random number and verify code setting
- About message
- imx6-uboot添加lvds1显示
- Beyondcompare 4 uses PJ
猜你喜欢

Redis的主从复制原理

JS how to convert a string into an array object

Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference

Beyondcompare 4 uses PJ

KDD2022 | 边信息增强图Transformer

7-5 complex quaternion operation

golang的channel和条件变量在单生产单消费场景下的性能对比测试

Open source project - (erp+ Hotel + e-commerce) background management system

【Leetcode】221. Largest Square

左右案例+小圆点的轮播图+无时间
随机推荐
Tron API wave field transfer query interface PHP version package based on thinkphp5 attached interface document 20220528 version
宏编译 预处理头 WIN32_LEAN_AND_MEAN
关系代数笛卡尔积和自然连接的例子
【Leetcode】79. Word search
無重複字符的最長字符串(LeetCode 3)
用cloneNode 克隆,解决id问题/方法 深复制和浅复制修改id的方法
Dom+js+ carousel map + no time
MySQL review
Stress - system pressure simulation tool
Rust language learning
LDAP和SSO集成能实现什么效果?
用vector保存对象时保存指针的优点, 以及reserve的使用
Problems encountered in installing canvas and errors encountered in running the project
sublime_ Textuse
JS pre parsing, object, new keyword
Decision tree of machine learning
爱可可AI前沿推介(6.12)
A short guide to SSH port forwarding
Invalid date of moment conversion timestamp
Chapter VI data type (V)