bloreo.blogg.se

Nodejs event loop
Nodejs event loop








nodejs event loop

nodejs event loop

So during this first iteration of the event loop, the first function was executed, the second function was scheduled, and the third function was executed. The second key principle to understand is that using setTimeout schedules the callback to be executed at a later time, which will always be at least during the next iteration of the event loop. Rather, that value represents the minimum amount of time that needs to elapse before the callback will be executed.

nodejs event loop

The first principle is that using the setTimeout method and providing a delay value doesn't mean that the callback function will be executed exactly after that number of milliseconds. There are a couple of key principles to understand here. Why is that? Why is the second function executed last? The functions are executed in this order: first, third, second. Here, we call our first function, schedule our second function using setTimeout with a delay of zero milliseconds, then call our third function. According to the docs, those queued callbacks are then "processed after the current operation is completed, regardless of the current phase of the event loop." Instead, whenever the process.nextTick method is called, it places its callbacks into a queue. That's because it's a special method that's not technically part of the Node.js event loop.

nodejs event loop

It’s interesting to note that process.nextTick isn't mentioned in any of these phases. Close callbacks: Close callbacks, like when a socket connection is destroyed, are executed during this phase.Check: Callbacks scheduled by setImmediate are executed during this phase.Poll: New I/O events are retrieved and I/O callbacks are executed during this phase (except for callbacks scheduled by timers, callbacks scheduled by setImmediate, and close callbacks because those are all handled in different phases).Idle, prepare: This phase is only used internally by Node.js.Pending callbacks: I/O callbacks that were previously deferred to the next loop iteration are executed during this phase.Timers: Callbacks scheduled by setTimeout and setInterval are executed during this phase.Let’s briefly look at what happens in each phase: The Node.js event loop’s order of operations (Source: Node.js docs)Īs you can see, there are six main phases in the Node.js event loop.

#Nodejs event loop code

We'll even deploy some working code to Heroku (an easy way to quickly deploy apps) to see it all in action. In this article, we’ll dive into the theory behind the Node.js event loop and then look at a few examples using setTimeout, setImmediate, and process.nextTick. The Node.js event loop follows many of the same patterns as the JavaScript event loop but works slightly differently, as it doesn’t interact with the DOM but does deal with things like input and output (I/O). Node.js, however, implements its own “ Node.js event loop” that is different from the regular “JavaScript event loop.” How confusing! In the browser, the event loop coordinates the execution of code between the call stack, web APIs, and the callback queue. JavaScript is single-threaded, so how does it handle asynchronous code without blocking the main thread while it waits for an action to complete? The key to understanding the asynchronous nature of JavaScript is understanding the event loop.










Nodejs event loop