Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了The Node.js Event Loop, Timers, and process.nextTick()大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_696_4@理解了Node.js Event Loop中的每个  Phases 会执行的 operations 就知道为什么 使用定时器如setTimeout()

@H_696_4@设定在 5ms后执行回调函数,但在第3ms时有一个 process.nextTick() 回调函数执行了4ms,则setTimeout()的回调

@H_696_4@实际被执行的时间是第7ms。

@H_696_4@

The Node.js Event Loop, Timers, and process.nextTick()


@H_696_4@note: each Box will be referred to as a "phase" of the event loop.

@H_696_4@

What is the Event Loop? @H_502_25@ @H_696_4@ @H_696_4@The event loop is what allows Node.js to perform non-blocking I/Ooperations — despite the fact that JavaScript issingle-threaded — by offloading operations to the system kernel whenever possible.

@H_696_4@Since most modern kernels are multi-threaded,they can handle multipLeoperations execuTing in the BACkground. When one of these operationscompletes,the kernel tells Node.js so that the appropriate callBACkmay be added to the poll queue to eventually be executed. We'll explainthis in further detail later in this topic.

@H_696_4@

@H_696_4@

Phases Overview @H_502_25@
  • timers: this phase executes callBACks scheduled by setTimeout() and seTinterval().
  • I/O callBACks: executes almost all callBACks with the exception of close callBACks,the ones scheduled by timers,and setImmediate().
  • idle,prepare: only used internally.
  • poll: retrieve new I/O events; node will block here when appropriate.
  • checksetImmediate() callBACks are invoked here.
  • close callBACks: e.g. socket.on('close',...).
@H_696_4@

between each run of the event loop,Node.js checks if it is waiTing for any asynchronous I/O or timers and shuts down cleanly if there are not any.

大佬总结

以上是大佬教程为你收集整理的The Node.js Event Loop, Timers, and process.nextTick()全部内容,希望文章能够帮你解决The Node.js Event Loop, Timers, and process.nextTick()所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。