当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
【Leetcode】221. Largest Square
Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference
imx6-uboot添加lvds1显示
stress - 系统压力模拟工具
Find the median of two ordered arrays (leetcode 4)
Chapter VI data type (V)
宏编译 预处理头 WIN32_LEAN_AND_MEAN
[译] QUIC Wire Layout Specification - Packet Types and Formats | QUIC协议标准中文翻译(2) 包类型和格式
LeetCode_字符串_简单_344.反转字符串
关系代数笛卡尔积和自然连接的例子
Take the web page animation effects that can be used. Don't you come and have a look?
You can't just use console Log ()?
Problems encountered in generating MP3 from text to speech through iFLYTEK voice API
导航中,添加边框影响布局的解决方法
Numpy数值计算基础
JS pre parsing, object, new keyword
【Leetcode】637. Layer average of binary tree
sublime_text使用
作物模型的区域模拟实现过程初探
Longest string without duplicate characters (leetcode 3)









