Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

宏任务与微任务的本质区别是什么?

宏任务与微任务的本质区别是什么?是否可以理解为浏览器在达到调用条件时自动调用的是宏任务,自己编写代码触发调用的回调属于微任务


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

可以这样理解 只有promise是微任务(还有其他的不常用),其他的都是宏任务

有一个队列,js执行,点击事件,setTimeout都是往队列里推一条任务,这就是你说的宏任务

在每个宏任务执行完成后会调用:本轮被加入进去的微任务

然后下一个宏任务

console.log('task 1')

Promise.resolve().then(() => {
    console.log('task 1之后的微任务')
    setTimeout(() => {
      console.log('task 3')
    }, 0)
})

setTimeout(() => {
    console.log('task 2')
}, 0)

console.log('task 1 end')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...