When Bert presented his keynote at Node Interactive Europe 2016 on Event Loop, he started by saying that most of event loops diagrams are WRONG. I’m guilty of using one of them in my talks. :)

This is it. In it, event loop is spinning data back at clients like hot-cakes.


How Node Event Loop REALLY Works: Or Why Most of the Event Loop Diagrams are WRO ...

His diagram is closer to the real stuff. In it, event loop starts, works and then quits eventually (pun intended).


How Node Event Loop REALLY Works: Or Why Most of the Event Loop Diagrams are WRO ...

So there are a few important steps:

[Sidenote]

Reading blog posts is good, but watching video courses is even better because they are more engaging.

A lot of developers complained that there is a lack of affordable quality video material on Node. It's distracting to watch to YouTube videos and insane to pay $500 for a Node video course!

Go check out Node University which has FREE video courses on Node: node.university .

[End of sidenote] you launch a script: node index.js setTimeout() and setInterval() kick in You have some code running Unicorn function (more on it later) More code setImmediate() which is not really immediate Code Clean up with close events Last code process exit if no refs or back to 2 if you have refs

Refs is a simple counter which increment when there’s a async callback to be executed and decrements when it’s finally executed. When working with stream callbacks (like on data), there’s only one reference increment.


How Node Event Loop REALLY Works: Or Why Most of the Event Loop Diagrams are WRO ...

Each of the JS code blocks have its own process.nextTick() :


How Node Event Loop REALLY Works: Or Why Most of the Event Loop Diagrams are WRO ...

Yes. nextTick() should really be immediate and setImmediate() should be next tick. Argh. Just remember them as opposites. In other words, if all you have is bunch of nextTick() callbacks, your code will be run in that JS block and never go to the next event loop cycle (steps 2 9).

Sidenote: If you like this post and interested in a corporate on-site javascript, Node.js and React.js training to boost productivity of your team, then contactNodeProgram.com.

Finally, there’s the unicorn function. It’s like a communicator between Node and OS. You see, OS and its processes are async but they look like synchronous. This unicorn taps into the async nature and leverages it for Node’s event loop. Plus, unicorns are cool:


How Node Event Loop REALLY Works: Or Why Most of the Event Loop Diagrams are WRO ...
title

By the way, despite what diagrams are depicting, thread pool is not making network requests or performing sockets TCP work. The latter happens in Kernel.


How Node Event Loop REALLY Works: Or Why Most of the Event Loop Diagrams are WRO ...

Table flip? Exactly. But the nice thing about Node is for most of the things like building an Express server or running a Gulp task, you don’t need to know how event loop actually works!

Many thanks to Bert. This was one of my favorite talks at Node Interactive EU 2016. Now I’m going to re-draw my diagram to more appropriately illustrate how event loop really works in Node. :) How about this one?


How Node Event Loop REALLY Works: Or Why Most of the Event Loop Diagrams are WRO ...

--

Azat Mardan

https://www.linkedin.com/in/azatm

To contact Azat, the main author of this blog, submitthe contact form.

Also, make sure to get 3 amazing resources to FREE when you sign up for the newsletter.

Simple.

Easy.

No commitment.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: ReactJavaScriptGulpJavaNode.js
分页:12
转载请注明
本文标题:How Node Event Loop REALLY Works: Or Why Most of the Event Loop Diagrams are WRO ...
本站链接:http://www.codesec.net/view/522821.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(77)