DEV Community

Paul
Paul

Posted on

how node.js code execution with event loop works

most develop focus mainly on writing code, and the code execution part become mysterious, anyway if you want to know how javascript code is executed you can read this article on Deepak Gupta post it cover almost everything yo would like to know, lets talk about event loop now
javaScript code runs single threaded. so that means there is only one thing happening at a time, here is where event loops come in, in most browsers there is an event loop for every browser tab, to make every process isolated and avoid a web page with infinite loops or heavy processing to block your entire browser, for codes that takes too long to return back for example a set-timeout function , will cause the event loop to skip the execution of that certain JavaScript code in the page and add it to the event loop part of execution where it get executed after by setting it to the callback queue , you can visualize it bellow and more information on this youtube video by JSconf channel

Alt text of image

so as wikipedia states that " event loop is simply a programming construct or design pattern that waits for and dispatches events or messages in a program. The event loop works by making a request to some internal or external "event provider", then calls the relevant event handler "

concluding you have to know that event loop mostly operates asynchronously ,monitors call stack and the callback queue to call the relevant event handler

Top comments (0)