【js执行机制】

标题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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值