当前位置:网站首页>宏任务、微任务理解
宏任务、微任务理解
2022-06-27 19:45:00 【你一定要努力,但千万别着急】
1. 什么是宏任务 微任务
js 是一种单线程语言 简单来说 只有一条通道 在任务多的情况下 就会出现拥挤的情况 这种情况就产生了 ‘多线程’ 但是这种 ‘多线程’ 是通过单线程模仿的 也就是假的 那么就产生了 同步、异步任务。
2.JS 为什么要区分 宏任务、微任务
1 js是单线程的 但分同步异步
2 宏任务、微任务皆为 异步任务 它们都属于一个队列
3. 宏任务、微任务 有哪些
宏任务 script、setTimeout、setInterval、postMessage、MessageChannel、setImmediate
微任务 Promise.then、Object.observe、MutationObserver、process.nextTick
4. 宏任务、微任务 怎么执行的
先执行同步代码
遇到 异步宏任务 则将 异步宏任务 放入 宏任务队列中
遇到 异步微任务 则将 异步微任务 放入 微任务队列中
当所有同步代码执行完毕后
再将 异步微任务 从队列中调入 主线程执行
微任务执行完毕后
再将 异步宏任务 从队列中调入 主线程执行
一直循环 直至 所有任务执行完毕
5. 案例
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
- 遇到 setTimeout 异步宏任务 放入宏任务队列中
- 遇到 new Promise new Promise 在实例化时所执行的代码是同步 所以输出2
- Promise then 异步微任务 放入微任务队列中
- 遇到同步任务 输出5 主线程中同步任务执行完
- 将 异步微任务 从队列中调入 主线程执行 输出3 4 微任务队列为空
- 将 异步宏任务 从队列中调入 主线程执行 输出1 宏任务队列为空
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
- 遇到 setTimeout 异步宏任务 放入宏任务队列中
- 遇到 new Promse new Promse 在实例化的时候 所执行的代码是同步 所以输出1
- 而 Promise.then 异步微任务 放入微任务队列中
- 遇到同步任务 输出2 主线程中同步任务执行完
- 将 异步微任务 从队列中调入 主线程执行 输出3
此微任务中 又有微任务 Promise.resolve().then(微任务A).then(微任务B) 将其依次放入微任务队列中- 从微任务队列中 取出 微任务A 放入 主线程中 输出 before timeout
- 从微任务队列中 取出 微任务B 放入 主线程中 微任务B 又有 微任务C 放入微任务队列中
- 从微任务队列中 取出 微任务C 放入 主线程中 输出 also before timeout 微任务队列为空
- 从宏任务队列中 取出 宏任务 放入 主线程中 此任务 有一个微任务D 放入微任务队列中 接下来输出4 宏任务队列为空
- 从微任务队列中 取出 微任务D 放入 主线程中 输出 test 微任务队列为空
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
- 遇到同步任务 输出 1
- 遇到 setTimeout 异步宏任务 放入宏任务队列中
- 遇到 Promise new Promise 在实例化的时候所执行的代码是同步 但由于 new Promse 没有输出事件 所以接着执行 遇到 .then
- 执行 .then 异步微任务 放入 微任务队列中
- 遇到同步任务 输出3 主线程中同步任务执行完
- 从 微任务队列中 取出 微任务 放入 主线程中 输出 4 主线程中微任务执行完
- 从 宏任务队列中 取出 宏任务 放入 主线程中 输出 2 主线程中宏任务执行完
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
- 遇到同步任务 输出1
- 遇到 setTimeout 异步宏任务 放入宏任务队列中
- 遇到 Promise new Promise 在实例化的时候所执行的代码是同步 所以输出5 所以接着执行 遇到 .then
- 执行 .then 异步微任务 放入 微任务队列 中
- 遇到 setTimeout 异步宏任务 放入 宏任务队列中
- 遇到同步任务 输出10 主线程中同步任务执行完
- 从 微任务队列中 取出 微任务 放入 主线程中 输出 6 主线程中微任务执行完
- 从 宏任务队列中 取出 宏任务 放入 主线程中 执行第一个 输出 2 3 4
- 再执行第二个 setTimeout 输出 7 8 9 主线程中宏任务执行完
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
- 遇到 Promise new Promise 在实例化的时候所执行的代码是同步 但由于 没有输出事件
所以接着往下执行 遇到 new Promse 没有输出事件 再接着往下执行 遇到 .then 异步微任务 放入微任务队列中- 再接着 .then 异步微任务 放入微任务队列中
- 遇到同步任务 输出 3 主线程中同步任务执行完
- 从微任务队列中 取出 微任务 放入 主线程 中 输出 2 3 主线程中微任务执行完 任务队列为空
总结
先同步后异步
异步 包含 宏任务 微任务
异步 遇到 微任务 先执行微任务 执行完后 如果没有微任务 就执行下一个宏任务
边栏推荐
- How to design an elegant caching function
- Gao fushuai in the unit testing industry, pytest framework, hands-on teaching, will do this in the future test reports~
- [LeetCode]513. Find the value in the lower left corner of the tree
- Quick excel export
- STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
- Experience sharing of meituan 20K Software Test Engineers
- Matlab finds the position of a row or column in the matrix
- gomock mockgen : unknown embedded interface
- Special tutorial - Captain selection game
- 不外泄的测试用例设计秘籍--模块测试
猜你喜欢

Bit. Store: long bear market, stable stacking products may become the main theme

管理系统-ITclub(上)

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

∫(0→1) ln(1+x) / (x ² + 1) dx

不外泄的测试用例设计秘籍--模块测试

Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app

Stm32cubeide1.9.0\stm32cubemx 6.5 f429igt6 plus lan8720a, configure eth+lwip

真香,自从用了Charles,Fiddler已经被我彻底卸载了

Exclusive interview with millions of annual salary. What should developers do if they don't fix bugs?

Experience sharing of meituan 20K Software Test Engineers
随机推荐
YOLOv6:又快又准的目标检测框架开源啦
QT large file generation MD5 check code
记一次List对象遍历及float类型判断大小
[LeetCode]动态规划解分割数组II[Arctic Fox]
STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP
[Sword Offer II]剑指 Offer II 029. 排序的循环链表
GBase 8a数据库用户密码安全相关参数汇总
[LeetCode]572. A subtree of another tree
Codeforces Round #716 (Div. 2)
Experience sharing of meituan 20K Software Test Engineers
[LeetCode]508. 出现次数最多的子树元素和
Acwing week 57 longest continuous subsequence - (binary or tree array)
如何做好功能测试?你确定不想知道吗?
Go from introduction to practice -- shared memory concurrency mechanism (notes)
Method of reading file contents by Excel
matlab查找某一行或者某一列在矩阵中的位置
深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
[LeetCode]30. Concatenate substrings of all words
Go from introduction to actual combat - task cancellation (note)