I would like to understand the event-loop module - exactly what's going on behind the scene (OS wise, Thread wise) in node.js What happen when someone write to a file and apply a callback. how does the setTimeout work?
Asked
Active
Viewed 3,978 times
14
-
I'm not sure if you're after more detail, but this SO question covers some stuff pretty well (http://stackoverflow.com/questions/6645816/dont-understand-the-callback-and-non-blocking-example-node-js/6645888#6645888). My answer has some useful links in it. – danjah Sep 20 '11 at 09:05
-
1I am trying to understand what's going on behind the scene and not how to write to node.js...e.g. I'm trying to understand how libev works (the general idea...) – ciochPep Sep 20 '11 at 10:55
-
Try to look at these articles and presentation: * [Understanding the node.js event loop](http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/) * [Understanding Event-driven Programming](http://code.danyork.com/2011/01/25/node-js-doctors-offices-and-fast-food-restaurants-understanding-event-driven-programming/) * [JS Conf 2011 presentation](http://blip.tv/jsconf/jsconf2011-tom-hughes-croucher-5478056) – yojimbo87 Sep 20 '11 at 09:10
-
I find this blog post very informative as well [The JavaScript Event Loop: Explained](http://blog.carbonfive.com/2013/10/27/the-javascript-event-loop-explained/) – Gustav Oct 10 '14 at 08:15
-
Here is the sample demo you can modify the code and see how it's get executed : http://latentflip.com/loupe/?code=JC5vbignYnV0dG9uJywgJ2NsaWNrJywgZnVuY3Rpb24gb25DbGljaygpIHsKICAgIHNldFRpbWVvdXQoZnVuY3Rpb24gdGltZXIoKSB7CiAgICAgICAgY29uc29sZS5sb2coJ1lvdSBjbGlja2VkIHRoZSBidXR0b24hJyk7ICAgIAogICAgfSwgMjAwMCk7Cn0pOwoKY29uc29sZS5sb2coIkhpISIpOwoKc2V0VGltZW91dChmdW5jdGlvbiB0aW1lb3V0KCkgewogICAgY29uc29sZS5sb2coIkNsaWNrIHRoZSBidXR0b24hIik7Cn0sIDUwMDApOwoKY29uc29sZS5sb2coIldlbGNvbWUgdG8gbG91cGUuIik7!!!PGJ1dHRvbj5DbGljayBtZSE8L2J1dHRvbj4%3D – Nilesh Modak Aug 30 '19 at 05:47
1 Answers
0
The event loop mechanism is almost same on frontend and backend if you're using Node or JS. The setTimeout is not part of JS or Node, this method is part of browser API (frontend) and V8 engine(backend). You can understand it visually with this video by Phillip: https://www.youtube.com/watch?v=8aGhZQkoFbQ
Once, the broader concept is clear, you can read more about Microtasks
, https://javascript.info/microtask-queue

Akansh
- 1,715
- 3
- 15
- 34