当前位置:网站首页>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 .
边栏推荐
- Difference between Definition and Declaration
- Chapter VI data type (V)
- MVC mode, encryption, jsonwebtoken
- sublime_ Textuse
- LeetCode_字符串_简单_344.反转字符串
- TRON-api-波场转账查询接口-PHP版本-基于ThinkPHP5封装-附带接口文档-20220528版本
- MVC模式、加密、jsonwebtoken
- Start with Xiaobai, take the weight parameter from the trained model and draw the histogram
- 左右案例+小圆点的轮播图+无时间
- Shielding does not display vs warning
猜你喜欢

The second day of QML study

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

JS built in object

Promise understanding has used promise to realize picture preloading (sequential loading)

Reasons for college students' leave

7-5 complex quaternion operation

Performance comparison test of channel and condition variables of golang in single production and single consumption scenarios

寻找两个有序数组的中位数(LeetCode 4)

Cookies and sessions

Longest string without duplicate characters (leetcode 3)
随机推荐
Open source project - (erp+ Hotel + e-commerce) background management system
Records of gdcpc Guangdong Provincial Games in 22 years
sublime_ Textuse
服务端渲染与客户端渲染的区别(优缺点)
[译] Go References - The Go Memory Model | golang官方文档中文翻译之内存模型
Stress - system pressure simulation tool
Basic concepts of machine learning
bind、call、apply三者的区别,还有bind()的封装
[JS] some handwriting functions: deep copy, bind, debounce, etc
Performance comparison test of channel and condition variables of golang in single production and single consumption scenarios
A. Prefix range
鸡尾酒排序
【Leetcode】199. Right view of binary tree
NDT registration principle
QML first day
点云配准--gicp原理与其在pcl中的使用
You can't just use console Log ()?
Pre order, middle order and post order traversal of tree
LeetCode_字符串_简单_344.反转字符串
Automatic generation of folder directory structure