当前位置:网站首页>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 .
边栏推荐
- Rust language learning
- Suggestions and skills for advanced version of go language test
- Rust语言学习
- win7注册进程外组件, 服务, 以及COM组件调试
- 22年gdcpc广东省赛记录
- Matlab install license manager error -8
- Principle of master-slave replication of redis
- What can LDAP and SSO integration achieve?
- Preliminary study on Regional Simulation of crop model
- 关于报文
猜你喜欢

Video speed doubling in PC browser

You can't just use console Log ()?

Find the median of two ordered arrays (leetcode 4)

Redis的主从复制原理

JS built in object

Problems encountered in installing canvas and errors encountered in running the project

JS how to convert a string into an array object

JS string array converted to numeric array and how to add the numbers in the array

Principle of master-slave replication of redis

你不会只会用console.log()吧?
随机推荐
The second day of QML study
获取本机所有ipv4, ipv6地址
恭喜Splashtop 荣获2022年 IT Europa “年度垂直应用解决方案”奖
Beyondcompare 4 uses PJ
Autolock solves the problem of forgetting to unlock after locking
Advantages and disadvantages of single page development and multi page development
ELK搭建指南
Clone with cloneNode to solve the id problem / methods deep copy and shallow copy to modify the ID
Matlab install license manager error -8
Linear model of machine learning
Click to generate 4-bit random number and verify code setting
Traditional DOM rendering?
元宇宙是短炒,还是未来趋势?
B. Wall painting (C language)
[JS] some handwriting functions: deep copy, bind, debounce, etc
bind、call、apply三者的区别,还有bind()的封装
Backtracking, eight queens
Left and right cases + rotating pictures of small dots + no time
Rust language learning
安装canvas遇到的问题和运行项目遇到的报错