当前位置:网站首页>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
边栏推荐
- Go from introduction to actual combat - task cancellation (note)
- How to design an elegant caching function
- [LeetCode]动态规划解分割数组I[Red Fox]
- [LeetCode]508. 出现次数最多的子树元素和
- Windwos 8.1系统安装vmware tool插件报错的解决方法
- A method of go accessing gbase 8A database
- 真香,自从用了Charles,Fiddler已经被我彻底卸载了
- .NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法
- [LeetCode]100. Same tree
- Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"
猜你喜欢
Open source technology exchange - Introduction to Chengying, a one-stop fully automated operation and maintenance manager
.NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法
登录凭证(cookie+session和Token令牌)
Summary of Web testing and app testing by bat testing experts
畅游动态规划之区间DP
The create database of gbase 8A takes a long time to query and is suspected to be stuck
Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app
Codeforces Round #723 (Div. 2)
I think I should start writing my own blog.
STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
随机推荐
Bean paste green protects your eyes
Fill in the blank of rich text test
QT base64 encryption and decryption
大厂常用软件测试面试题三(附答案)
Go from introduction to practice -- shared memory concurrency mechanism (notes)
Luogu p5706 redistributing fertilizer and house water
Quick excel export according to customized excel Title Template
A method of go accessing gbase 8A database
Go from introduction to practice -- definition and implementation of behavior (notes)
Example of using gbase 8A OLAP function group by grouping sets
管理系统-ITclub(上)
Day8 - cloud information project introduction and creation
对话乔心昱:用户是魏牌的产品经理,零焦虑定义豪华
.NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法
Gbase 8A OLAP analysis function cume_ Example of dist
软件测试自动化测试之——接口测试从入门到精通,每天学习一点点
[LeetCode]100. Same tree
Acwing week 57 longest continuous subsequence - (binary or tree array)
Crontab scheduled task common commands
xpath