代码轮询方式:
1.先执行同步代码
2.一旦遇到异步代码,进行任务类型判断,分别根据类型放在宏任务和微任务
3.等同步代码执行完后,先去微任务拉任务来执行,再去宏任务执行任务
console.log(1)
setTImeOut(()=>{
console.log(2)
})
new Promise(resolve => {
console.log(3)
}).then(()=>{
console.log(4)
})
console.log(5)
执行顺序:1 , 3 , 5, 4 ,2
分析:1 是同步,3(new Promise是构造函数也是同步),5是同步,4 then() 是微任务,setTimeOut 是宏任务
宏任务常见的:setTimeOut setInterval
微任务常见的:then() $.nextTick()