node-in-debugging
node-in-debugging copied to clipboard
前者表述的含义setTimeout和setInterval在timers阶段执行,后者表示setTimeout和setInterval在poll阶段执行,是否自相矛盾?
你的文章中写道下面的两个, 前者表述的含义setTimeout和setInterval在timers阶段执行,后者表示setTimeout和setInterval在poll阶段执行,是否自相矛盾?
每个阶段(phase)的作用:timers:执行 setTimeout() 和 setInterval() 中到期的 callback。
poll 阶段主要有两个功能: 当 timers 的定时器到期后,执行定时器(setTimeout 和 setInterval)的 callback。 执行 poll 队列里面的 I/O callback。
我们看nodejs官方的解释: 这个解释的含义应该是: poll阶段会取回所有的io事件, 执行相关的io回调(换句话说:是除了timers和setimmediate之外的所有事件)。 按照官方的解释,poll阶段并不会执行定时器相关的回调。
poll: retrieve new I/O events; execute I/O related callbacks (almost all with the exception of close callbacks, the ones scheduled by timers, and setImmediate()); node will block here when appropriate. https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/