Morning Keynote- Everything You Need to Know About Node.js Event Loop - Bert Belder, IBM

node.js · 107K views · 2016 · 15:42

Open on YouTube

Web developmentPython, Ruby & scripting languagestalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about how the Node.js event loop decides to keep running or exit, since it comes from one of libuv's authors.

Recommended by 7 people across 7 different months, 2017 to 2023.

What it is

A 15-minute keynote at Node.js Interactive 2016 in which Bert Belder, a libuv author working at IBM, explains how the Node.js event loop actually works. He covers common misconceptions such as whether Node uses threads, where callbacks come from, and how nextTick compares to setImmediate.

What people said

Recommends Mark · 2017-10 · Stack Overflow · on “what happens when a Promise never resolves?”

My closest guess … it keeps track of the active things happening in your code, when there is nothing else happening, it simply exits. This is essentially correct. Node keeps a reference count of things like timers and network requests. When you make a network, or other async request, set a timer, etc. Node adds on to this ref count. When the times/request resolve Node subtracts from the count. This count is how Node decides whether to exit at the end of the event loop. When you get to the end of the event loop Node looks at this count and if it's zero exits. Simply making a promise, doesn't add to the ref count because it's not an async request. There's a good talk about some of this by [Nod

Recommends Alex D · 2018-07 · Stack Overflow · on “How does Node keep a server process alive?”

The real answer finally came from Bert Belder [this video] , who worked on libuv. Every time Node queues an action that will resolve at some point in the future, think async functions or timers, it has a ref – a mark to remind itself it’s waiting on some future work. The event loop checks if there are any refs. If the ref counter is 0 , Node exits the process. If there are refs, the event loop goes back to the start and does its thing. You can think of it as a super simple while(refCounter) loop. So, what happens with a server? This exact process, except the listen function doesn’t decrement the ref counter. The event loop sees there’s always at least one ref left, so it never ends the process in

Recommends palo · 2019-03 · Stack Overflow · on “Understanding Node.Js Event Loop Execution”

Let me try You can find brief (16 minutes) explanation about the node's event loop here: [this video] The official guide (mentioned by @estus) is the best explanation: [LINK] For more in depth explanation, check this series of blogposts: [LINK] According the official guide [LINK] : You may have noticed that process.nextTick() was not displayed in the diagram, even though it's a part of the asynchronous API. This is because process.nextTick() is not technically part of the event loop. Instead, the nextTickQueue will be processed after the current operation is completed, regardless of the current phase of the event loop. Looking back at our diagram, any time you call process.nextTick() in a given p

Recommends user1060654 · 2019-05 · Stack Overflow · on “NodeJs & Socket.IO turn based multiplayer game, question about pairing players”

You can run into a "race condition", it's explained here in this package which provides you a Locking mechanism. [LINK] That package will be useful, only if you run node.js in a single process, meaning you are not having multiple servers, or having node cluster running multiple processes. In that case, you will have to implement a distributed locking mechanism which is one of the most complex things in distributed computing, but today you can use the npm package for Redlock algorithm, set 3 redis servers and go. Too much overhead for a game without players. Node.js is not single threaded, here is the explanation of one of the creators. Morning Keynote- Everything You Need to Know About Node.

Recommends Kane Hooper · 2019-07 · Stack Overflow · on “What is the different between JavaScript Event loop and Node.js Event loop?”

Sorry. But on this you are incorrect. You don't have to believe me, but if you watch this video from Bert Belder, one of the co-authors of the Nodejs event loop library (Libuv) he talks about this exact point. [this video] Nodejs uses a different event loop model than ECMAScript compatible browsers. The links I have provided make this clear.

Recommends confusedWarrior · 2021-01 · Stack Overflow · on “Does event loops polls for event completion or kernel/os notifies back?”

When Node.js starts, it initializes the event loop, processes the provided input script which may make async API calls, schedule timers, or call process.nextTick(), then begins processing the event loop. There are seven phases and each phase has its own event queue which is based on FIFO. So application makes a request event, event demultiplexer gathers those requests and pushes to respective event queues. For example, If my code makes two reqeusts one is setTimeOut() and another is some API Call , demultiplexer will push the first one in timer queue and other in poll queue. But events are there, and loop watches over those queues and events, on completion in pushes the registered callback t

Recommends Blind Kai · 2023-03 · Stack Overflow · on “What does 'spin the event loop' mean?”

I will recommend you to read some official guides about Event Loop: [LINK] [LINK] And to check some talks about the Event Loop: [LINK] [this video] Answering your question: The event loop "spin" means that the Event loop performed run through all queues. After its run, the EL checks if there are active handles or some requests or timers which are still processing or haven't been done yet. In other words, a "spin" means that the Event Loop have done a single iteration.

Cites Mark · 2017-10 · Stack Overflow · on “Node exits without error and doesn't await promise (Event callback)”

This question is basically: how does node decide whether to exit the event loop or go around again? Basically node keeps a reference count of scheduled async requests — setTimeouts , network requests, etc.. Each time one is scheduled, that count increases, and each time one is finished, the count decreases. If you arrive at the end of an event loop cycle and that reference count is zero node exits. Simply creating a promise or event emitter does not increase the reference count — creating these objects isn't actually an async operation. For example, this promise's state will always be pending but the process exits right away: const p = new Promise( resolve => { if(false) resolve() }) p.then(

Cites Ajmal Nasumudeen · 2022-04 · Stack Overflow · on “how nodejs work? how eventloop works? what is worker thread?”

How nodejs work? I read the nodejs official documentation many times, i couldn't understand what exactly the meant, What I understood is, whenever a nodejs program starts the eventloop also starts. Offical Documentations says The event loop is what allows Node.js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible. Event loop has 5 phases, Timers Pending Callbacks Idle-Prepare (Dont know what exactly its is) Poll Check Close Callbacks There is a slight discrepancy between the Windows and the Unix/Linux implementation, [this video] I viewed above video many times, its very difficult to unders

Cites Shankar Regmi · 2023-02 · Stack Overflow · on “JavaScript returning new Promise without resolve() statement, doesn't await as expected”

This is what's happening in your case: Your program starts it's execution from an anonymous IIFE async function, as this is an async function, it immediately returns a Promise to a global scope.So the execution of anonymous IIFE is deferred . You can easily validate this by adding a console.log("OK"); at the end of your IIFE invocation, which is printed to the console Node keeps a reference count of things like timers and network requests. When you make a network, or other async request, set a timer, etc. Node adds on to this ref count. When the times/request resolve Node subtracts from the count. Ref. video link [this video] So what happens inside your IIFE is: console.log("START"); <--- gets pr