当前位置:网站首页>Macro task and micro task understanding
Macro task and micro task understanding
2022-06-27 22:17:00 【You must try, but don't worry】
1. What is a macro task Micro task
js It's a single thread language Simply speaking There is only one channel When there are many tasks There will be congestion This situation arises ‘ Multithreading ’ But this kind of ‘ Multithreading ’ It is imitated by single thread That's fake Then there is Sync 、 Asynchronous task .
2.JS Why distinguish Macro task 、 Micro task
1 js It's single threaded But sub synchronous asynchronous
2 Macro task 、 All micro tasks are Asynchronous task They all belong to a queue
3. Macro task 、 Micro task What are they?
Macro task script、setTimeout、setInterval、postMessage、MessageChannel、setImmediate
Micro task Promise.then、Object.observe、MutationObserver、process.nextTick
4. Macro task 、 Micro task How to execute
Execute synchronization code first
encounter Asynchronous macro task Will Asynchronous macro task Put in Macro task queue
encounter Asynchronous micro task Will Asynchronous micro task Put in In the micro task queue
When all synchronization code is executed
then Asynchronous micro task Call in from the queue Main thread execution
When the micro task is completed
then Asynchronous macro task Call in from the queue Main thread execution
Cycle all the time until All tasks are completed
5. Case study
setTimeout(function(){
console.log(1)
})
new Promise(function(resolve){
console.log(2)
resolve()
}).then(function(){
console.log(3)
}).then(function(){
console.log(4)
})
console.log(5)
// 2 5 3 4 1
- encounter setTimeout Asynchronous macro task Put in the macro task queue
- encounter new Promise new Promise The code executed when instantiating is synchronous So the output 2
- Promise then Asynchronous micro task Put it in the queue of microtasks
- Encounter synchronization task Output 5 The synchronization task in the main thread has finished executing
- take Asynchronous micro task Call in from the queue Main thread execution Output 3 4 The micro task queue is empty
- take Asynchronous macro task Call in from the queue Main thread execution Output 1 Macro task queue is empty
setTimeout(() => {
new Promise(resolve => {
resolve()
}).then(() => {
console.log('test')
})
console.log(4)
})
new Promise(resolve => {
resolve()
console.log(1)
}).then( () => {
console.log(3)
Promise.resolve().then(() => {
console.log('before timeout')
}).then(() => {
Promise.resolve().then(() => {
console.log('also before timeout')
})
})
})
console.log(2)
// 1, 2, 3, before timeout, also before timeout, 4, test
- encounter setTimeout Asynchronous macro task Put in the macro task queue
- encounter new Promse new Promse When instantiating The code executed is synchronized So the output 1
- and Promise.then Asynchronous micro task Put it in the queue of microtasks
- Encounter synchronization task Output 2 The synchronization task in the main thread has finished executing
- take Asynchronous micro task Call in from the queue Main thread execution Output 3
In this micro task There are micro tasks Promise.resolve().then( Micro task A).then( Micro task B) Put them in the micro task queue in turn- From the micro task queue Take out Micro task A Put in In the main thread Output before timeout
- From the micro task queue Take out Micro task B Put in In the main thread Micro task B And then there is Micro task C Put it in the queue of microtasks
- From the micro task queue Take out Micro task C Put in In the main thread Output also before timeout The micro task queue is empty
- From the macro task queue Take out Macro task Put in In the main thread This task There is a micro task D Put it in the queue of microtasks Next, output 4 Macro task queue is empty
- From the micro task queue Take out Micro task D Put in In the main thread Output test The micro task queue is empty
console.log(1)
setTimeout(function() {
console.log(2)
}, 0)
const p = new Promise((resolve, reject) => {
resolve(4)
})
p.then(data => {
console.log(data)
})
console.log(3)
// 1, 3, 4, 2
- Encounter synchronization task Output 1
- encounter setTimeout Asynchronous macro task Put in the macro task queue
- encounter Promise new Promise The code executed during instantiation is synchronized But because of new Promse No output events So go ahead and do encounter .then
- perform .then Asynchronous micro task Put in In the micro task queue
- Encounter synchronization task Output 3 The synchronization task in the main thread has finished executing
- from In the micro task queue Take out Micro task Put in In the main thread Output 4 The micro task in the main thread has finished executing
- from Macro task queue Take out Macro task Put in In the main thread Output 2 The macro task in the main thread has finished executing
console.log(1)
setTimeout(function() {
console.log(2)
new Promise(function(resolve) {
console.log(3)
resolve()
}).then(function() {
console.log(4)
})
})
new Promise(function(resolve) {
console.log(5)
resolve()
}).then(function() {
console.log(6)
})
setTimeout(function() {
console.log(7)
new Promise(function(resolve) {
console.log(8)
resolve()
}).then(function() {
console.log(9)
})
})
console.log(10)
// 1, 5, 10, 6, 2, 3, 4, 7, 8, 9
- Encounter synchronization task Output 1
- encounter setTimeout Asynchronous macro task Put in the macro task queue
- encounter Promise new Promise The code executed during instantiation is synchronized So the output 5 So go ahead and do encounter .then
- perform .then Asynchronous micro task Put in Micro task queue in
- encounter setTimeout Asynchronous macro task Put in Macro task queue
- Encounter synchronization task Output 10 The synchronization task in the main thread has finished executing
- from In the micro task queue Take out Micro task Put in In the main thread Output 6 The micro task in the main thread has finished executing
- from Macro task queue Take out Macro task Put in In the main thread Execute the first Output 2 3 4
- Carry out the second setTimeout Output 7 8 9 The macro task in the main thread has finished executing
new Promise((resolve, reject) => {
resolve(1)
new Promise((resolve, reject) => {
resolve(2)
}).then(data => {
console.log(data)
})
}).then(data => {
console.log(data)
})
console.log(3)
// 3, 2, 1
- encounter Promise new Promise The code executed during instantiation is synchronized But because of No output events
So go on encounter new Promse No output events And then go on encounter .then Asynchronous micro task Put it in the queue of microtasks- We'll go on with the .then Asynchronous micro task Put it in the queue of microtasks
- Encounter synchronization task Output 3 The synchronization task in the main thread has finished executing
- From the micro task queue Take out Micro task Put in The main thread in Output 2 3 The micro task in the main thread has finished executing Task queue is empty
summary
First synchronous then asynchronous
asynchronous contain Macro task Micro task
asynchronous encounter Micro task Do the micro task first After the execution If there is no micro task Just go to the next macro task
边栏推荐
- Introduction to ARCS Model
- Stm32cubeide1.9.0\stm32cubemx 6.5 f429igt6 plus lan8720a, configure eth+lwip
- [LeetCode]513. Find the value in the lower left corner of the tree
- AQS SOS AQS with me
- Hash table - sum of arrays
- YOLOv6:又快又准的目标检测框架开源啦
- GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析
- 美团20k软件测试工程师的经验分享
- Codeforces Round #717 (Div. 2)
- Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
猜你喜欢

Educational Codeforces Round 108 (Rated for Div. 2)

STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP

渗透学习-sql注入过程中遇到的问题-针对sort=left(version(),1)的解释-对order by后接字符串的理解

Login credentials (cookie+session and token token)

渗透学习-靶场篇-pikachu靶场详细攻略(持续更新中-目前只更新sql注入部分)

The create database of gbase 8A takes a long time to query and is suspected to be stuck
![\w和[A-Za-z0-9_],\d和[0-9]等价吗?](/img/96/2649c9cf95b06887b57fd8af2d41c2.png)
\w和[A-Za-z0-9_],\d和[0-9]等价吗?

深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图

BAT测试专家对web测试和APP测试的总结

百万年薪独家专访,开发人员不修复bug怎么办?
随机推荐
畅游动态规划之区间DP
MONTHS_BETWEEN函数使用
. Net learning notes (V) -- lambda, LINQ, anonymous class (VaR), extension method
软件测试自动化测试之——接口测试从入门到精通,每天学习一点点
[LeetCode]508. 出現次數最多的子樹元素和
宏任务、微任务理解
Record a list object traversal and determine the size of the float type
使用Jmeter进行性能测试的这套步骤,涨薪2次,升职一次
[Sword Offer II]剑指 Offer II 029. 排序的循环链表
Crontab scheduled task common commands
【Redis】零基础十分钟学会Redis
BAT测试专家对web测试和APP测试的总结
Luogu p5706 redistributing fertilizer and house water
渗透学习-sql注入过程中遇到的问题-针对sort=left(version(),1)的解释-对order by后接字符串的理解
qt 大文件生成md5校验码
GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析
The create database of gbase 8A takes a long time to query and is suspected to be stuck
Go from introduction to actual combat - only any task is required to complete (notes)
[LeetCode]186. 翻转字符串里的单词 II
记一次List对象遍历及float类型判断大小