当前位置:网站首页>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
边栏推荐
- [LeetCode]186. 翻转字符串里的单词 II
- Gbase 8A OLAP analysis function cume_ Example of dist
- Codeforces Round #721 (Div. 2)
- 北京邮电大学|用于成本和延迟敏感的虚拟网络功能放置和路由的多智能体深度强化学习
- QT large file generation MD5 check code
- 软件缺陷管理——测试人员必会
- Crontab scheduled task common commands
- Yolov6: the fast and accurate target detection framework is open source
- STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
- 石子合并问题分析
猜你喜欢

Interval DP of Changyou dynamic programming

Système de gestion - itclub (II)
扁平数组和JSON树的转换

年薪50W+的测试大鸟都在用这个:Jmeter 脚本开发之——扩展函数

畅游动态规划之区间DP
![\W and [a-za-z0-9_], \Are D and [0-9] equivalent?](/img/96/2649c9cf95b06887b57fd8af2d41c2.png)
\W and [a-za-z0-9_], \Are D and [0-9] equivalent?

PCIe knowledge point -008: structure of PCIe switch

使用Fiddler模拟弱网测试(2G/3G)

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

开源技术交流丨一站式全自动化运维管家ChengYing入门介绍
随机推荐
【Redis】零基础十分钟学会Redis
使用Jmeter进行性能测试的这套步骤,涨薪2次,升职一次
Go from introduction to actual combat - execute only once (note)
Hash table - sum of arrays
Gao fushuai in the unit testing industry, pytest framework, hands-on teaching, will do this in the future test reports~
畅游动态规划之区间DP
PCIe knowledge point -008: structure of PCIe switch
管理系统-ITclub(中)
Go from introduction to practice -- definition and implementation of behavior (notes)
【mysql实战】查询语句实战演示
Management system itclub (medium)
渗透学习-sql注入过程中遇到的问题-针对sort=left(version(),1)的解释-对order by后接字符串的理解
I think I should start writing my own blog.
Analysis of stone merging
宏任务、微任务理解
It smells good. Since I used Charles, Fiddler has been completely uninstalled by me
Gbase 8A OLAP analysis function cume_ Example of dist
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
Codeforces Round #723 (Div. 2)
[LeetCode]513. 找树左下角的值