当前位置:网站首页>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
边栏推荐
猜你喜欢

6G显卡显存不足出现CUDA Error:out of memory解决办法

It smells good. Since I used Charles, Fiddler has been completely uninstalled by me

Luogu p5706 redistributing fertilizer and house water

Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"

crontab定时任务常用命令

Go from introduction to practice -- definition and implementation of behavior (notes)

Simulink导出FMU模型文件方法
![[LeetCode]动态规划解分割数组I[Red Fox]](/img/b2/df87c3138c28e83a8a58f80b2938b8.png)
[LeetCode]动态规划解分割数组I[Red Fox]

Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting

Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app
随机推荐
深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图
CDH集群之YARN性能调优
A method of go accessing gbase 8A database
[MySQL practice] query statement demonstration
Gao fushuai in the unit testing industry, pytest framework, hands-on teaching, will do this in the future test reports~
Experience sharing of meituan 20K Software Test Engineers
xpath
Go from introduction to actual combat - task cancellation (note)
[leetcode] 508. Élément de sous - arbre le plus fréquent et
渗透学习-靶场篇-pikachu靶场详细攻略(持续更新中-目前只更新sql注入部分)
渗透学习-sql注入过程中遇到的问题-针对sort=left(version(),1)的解释-对order by后接字符串的理解
It smells good. Since I used Charles, Fiddler has been completely uninstalled by me
Simulink导出FMU模型文件方法
Common problems encountered by burp Suite
Acwing week 57 longest continuous subsequence - (binary or tree array)
管理系统-ITclub(下)
管理系统-ITclub(中)
xpath
Day8 - cloud information project introduction and creation
Yolov6: the fast and accurate target detection framework is open source