宏任务和微任务,js队列执行顺序

微任务执行时机比宏任务早

宏任务: settimeout, setInterval, DOM事件, AJAX请求

微任务:Promise, async,await

1
2
3
4
5
6
7
8
9
10
11
console.log(1)
setTimeout(() => {
    console.log(2)
})
new Promise(function(resolve) {
    console.log('3')
    resolve()
}).then(function() {
    console.log('4')
})
console.log(5)

  输出结果为 1,3,5,4,2

解析: 首先执行同步js,promise本身也是同步的,但它的then,catch却是异步的,执行完同步后,将执行微任务,也就是promise的then部分,最后才是宏任务settimeout

posted @   乐得逍遥  阅读(381)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示