标题js执行机制
一、运行机制顺序:
1.同步程度
2.nextTick
3.异步
4.setImmediate(当前事件循环结束执行)
二、加上宏任务和微任务的执行顺序
1.同步程度
2.process.nextTick
3.微任务(promise.then,async)
4.宏任务(setTimeout,ajax,读取文件)
5.setImmediate(当前事件循环结束执行)
每次事件循环都看任务队列里面有没有东西,有就执行
利用代码来理解理解
setTimeout(function() {
console.log('1');
})
new Promise(function(resolve) {
console.log('2');
resolve(true)
}).then(function() {
console.log('3');
})
console.log('4');
//2,4,3,1
setImmediate(()=>{
console.log(1)
})
console.log(2)
setTiemout(function(){console.log(3)},0)
setTiemout(function(){console.log(4)},100)
console.log(5)
new Promise((resolve)=>{
console.log(6)//promise.then的这个6是同步代码
resolve()
}).then(()=>{{
console.log(7)
})
process.nextTict(()=>{
console.log(8)
})
//执行顺序:2 5 6 8 7 3 1 4