LuckyWinty / fe-weekly-questions

A pro to record some interview questions every week...
MIT License
341 stars 34 forks source link

在node中运行下面的代码会输出什么 #43

Open LuckyWinty opened 4 years ago

LuckyWinty commented 4 years ago
setTimeout(() => {
  console.log('setTimeout')
}, 0)

setImmediate(() => {
  console.log('setImmediate')
})

const start = Date.now()
while (Date.now() - start < 10);
LuckyWinty commented 4 years ago

输出为: setTimeout setImmediate

因为执行timers阶段前的时间大于1ms