宏任务&微任务

以chrome为例

setTimeout(function(){    console.log('setTimeout')},0)
new Promise(function(resolve){
    console.log('promise')
    resolve()
}).then(function(){
    console.log('then')
})复制代码

浏览器eventloop机制

js执行为单线程,因此分为 同步和异步

先执行同步任务,即 setTimeout ,  new Promise,  console.log('promise')

在异步时分为 宏任务 和 微任务,

setTimeout,setInterval等为宏任务,Promise为微任务。两个任务被挂载到不同的task queue中。

浏览器先执行微任务的queue,即console.log(‘then’)然后在执行 宏任务 console.log(‘setTimeoout’)


猜你喜欢

转载自blog.csdn.net/weixin_34366546/article/details/91374266