当前位置:网站首页>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
边栏推荐
- [sword offer ii] sword finger offer II 029 Sorted circular linked list
- [LeetCode]30. 串联所有单词的子串
- [LeetCode]100. Same tree
- crontab定时任务常用命令
- qt 大文件生成md5校验码
- Yarn中RMApp、RMAppAttempt、RMContainer和RMNode状态机及其状态转移
- 读写分离-Mysql的主从复制
- \w和[A-Za-z0-9_],\d和[0-9]等价吗?
- Use Fiddler to simulate weak network test (2g/3g)
- CUDA error:out of memory caused by insufficient video memory of 6G graphics card
猜你喜欢
How to design an elegant caching function

【MySQL】数据库函数通关教程下篇(窗口函数专题)
![\w和[A-Za-z0-9_],\d和[0-9]等价吗?](/img/96/2649c9cf95b06887b57fd8af2d41c2.png)
\w和[A-Za-z0-9_],\d和[0-9]等价吗?

Open source technology exchange - Introduction to Chengying, a one-stop fully automated operation and maintenance manager

美团20k软件测试工程师的经验分享

Go from introduction to actual combat - execute only once (note)

管理系统-ITclub(下)

登录凭证(cookie+session和Token令牌)

清华大学教授:软件测试已经走入一个误区——“非代码不可”

Yolov6: the fast and accurate target detection framework is open source
随机推荐
石子合并问题分析
Go from introduction to practice -- definition and implementation of behavior (notes)
\W and [a-za-z0-9_], \Are D and [0-9] equivalent?
二维数组中修改代价最小问题【转换题意+最短路径】(Dijkstra+01BFS)
How to do function test well? Are you sure you don't want to know?
Read write separation master-slave replication of MySQL
Codeforces Round #717 (Div. 2)
渗透学习-靶场篇-pikachu靶场详细攻略(持续更新中-目前只更新sql注入部分)
Interview question 3 of software test commonly used by large factories (with answers)
YOLOv6:又快又准的目标检测框架开源啦
Yarn中RMApp、RMAppAttempt、RMContainer和RMNode状态机及其状态转移
Use Fiddler to simulate weak network test (2g/3g)
[LeetCode]161. Edit distance of 1
Software defect management - a must for testers
[leetcode] 508. Élément de sous - arbre le plus fréquent et
Open source technology exchange - Introduction to Chengying, a one-stop fully automated operation and maintenance manager
Système de gestion - itclub (II)
Luogu p5706 redistributing fertilizer and house water
MYSQL和MongoDB的分析
[LeetCode]30. Concatenate substrings of all words