1. node.js初始化
初始化event loop -> 執(zhí)行server.js文件 -> 進入event loop
2. eventLoop循環(huán)過程
eventLoop.png
3. poll階段
poll.png
4. check階段
大部分情況都是先執(zhí)行check階段的setImmediate(),再執(zhí)行timer階段的setTimeout(),因為poll之后就是check,只有在第一次啟動的時候疤估!才可能會先執(zhí)行timer碧查。
因為setTimeout()的最短時間是4ms(即使你設置0)糊啡,而node.js的初始化過程是
初始化eventLoop -> 執(zhí)行server
.js -> 進入eventLoop颖对,在執(zhí)行server.js到進入eventLoop這段時間若大于4ms則會先執(zhí)行timer挺据,若沒到4ms的話嫁赏,就不會先執(zhí)行timer的setTimeout()其掂,
5. nextTick()
setTimeout(()=>{
console.log('1')
})
setImmediate(()=>{
console.log('2')
})
process.nextTick(()=>{
console.log('3')
})
會先執(zhí)行3,在進入eventLoop之前潦蝇,和eventLoop里進入每一個階段之前都會先調(diào)用一次nextTick()