Javascript Event Loop - Why so Serious!

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
Search for a command to run...

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
Thanks Jeremie Corpinot. Here you go: https://hashnode.com/post/task-queue-and-job-queue-deep-dive-into-javascript-event-loop-model-cjui19qqa005wdgs1742fa4wz
Traditional SSR is great, but it has a massive buffering problem. Let's look into the SSR Streaming and learn how it could be a game changer.

Learn how to build maintainable, reusable forms in React using design patterns like custom hooks, compound components, and context for scalable CRUD

Learn how JavaScript variables work, including let vs const, primitive and reference types, pass by value, and how JavaScript stores data in memory.

A beginner-friendly introduction to JavaScript covering what it is, how it works, and how to set up your JavaScript environment

Learn how the promises are handled internally using event loop. This concept is essentials to debug and work with async programming.

GreenRoots Blog - A Blog by Tapas Adhikary
186 posts
Educator | YouTuber | Building ReactPlay | Let's Connect
Let me put a dramatic start to this one.
This is not just a blog, story, or any other technical article. This is rather a realization that I had very recently. I have seen two categories of JavaScript developers in my surroundings. One that knows how to write a program(or application) with it. Another category of people who know(or have the thirst to know) how JavaScript was written fundamentally as a programming language. It is no good or bad between these two categories. However, it is mostly about the transition and balance between one another.
Now coming back to my own realization that I was talking about, it all started with the question I had, "What is Javascript"? I spent time searching for the answer over the internet, books and the best that I got so far(and by far) is:

The definition above opened the door for asking lots of other questions. Single-Threaded, How does it support asynchronous things? What makes it non-blocking and concurrent? How does it work internally? The shortest answer was Javascript's Event Loop Model. As I understood broadly, there are mainly three components to it for an execution context.
"A picture is worth a thousand words". Here is the arrangement of all these components in the Event Loop Model(At this point, you don't have to understand it fully. Keep Reading) :
(This Screenshot is taken from a fantastic online tool, http://latentflip.com/loupe/)
So how does it work altogether?

Let's explain this with a simple Code Execution.
const tom = () => console.log('tom');
const jerry= () => console.log('jerry');
const cartoon = () => {
console.log('cartoon ');
setTimeout(tom, 5000);
jerry();
}
cartoon ();
The Event Loop is in Action (Observe the sequence couple of times):
(Courtesy: http://latentflip.com/loupe/)

So what is happening there? Let us see step by step.
cartoon gets into the stack and it will be executed line by line.console.log('cartoon ') is pushed into the stack and executed.setTimeout(tom, 5000); will be handled outside of the Call Stack as it is a Web API. It has a Callback function called tom which will be en-queued in Task Queue.jerry() which gets executed.cartoon exited.tom gets executed in Stack Frame.That's all about it! The event Loop Model is really nothing that of so much difficulty and seriousness. It is about understanding how things work in an orchestrated manner. As I mentioned before, there are different types of queues, Task queues, and Job Queue. I have generalized it here as Queue. I shall be writing another post to explain the difference specifically.
Hope you liked the post, Stay Tuned!