Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    JavaScript

    10 Hiring Lessons from Silicon Valley: for Small and Large Companies

    JavaScript

    Minimize Downtime by Creating a Health-check for Your NodeJS Application

    Beginners

    Web Development Contract: Talent Agreement Essentials | Things to Include and Avoid

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, September 10
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / JavaScript / Node.js / Node.js Lessons / Nodejs Lesson 16: Internals of Nodejs: Event Loop
    Node.js

    Nodejs Lesson 16: Internals of Nodejs: Event Loop

    Mohammad Shad MirzaBy Mohammad Shad MirzaFebruary 5, 2021No Comments5 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    Nodejs Lesson 16: Internals of Nodejs: Event Loop
    Nodejs Lesson 16: Internals of Nodejs: Event Loop
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    Nodejs Lesson 16: Internals of Nodejs: Event Loop
    Nodejs Lesson 16: Internals of Nodejs: Event Loop

    Hey everyone, this lesson will continue the last lesson where we learned how the LibUV library works internally. In this lesson, we will learn about Event Loop and understand various terms like call stack, callback queue, etc. Let’s start.

    What is Call Stack

    JavaScript is a single-threaded language, which means it can do one thing at a time. This also means that it has a single Call Stack. Now, what is a Call Stack?

    A call stack is a data structure that holds information about what line of the function is currently being executed. A call stack is used to keep method calls, and it follows the LIFO (Last In First Out) data structure. By LIFO, we mean a container where the last item entered will be the first to get out. There is only one entry from the top, and if you want to take the item at the end, you will have to take everything on top first. If you’re thinking about a box of Pringles, then you’re absolutely correct.

    Let’s understand how this call stack holds method calls:

    Callback excallidraw example
    Callback excallidraw example

    The execution will go on like this:

    1. We call the main() function, it gets pushed into the call stack.
    2. This will call the add() function and push it to call stack.
    3. Once it hit return, add() will be popped out from the call stack.
    4. Finally, the main() will be popped out.

     

    This will leave the call stack empty, just like how it was at the beginning. I hope things are clear until now.

    Think about what will happen if one of the expressions takes a long time to run? Something like a very long for a loop. Call stack can only accept new expression once the current one has finished running. And since the for loop is still running, javascript can no longer accept new expressions.

    The thread will be busy, and it will no longer execute further, leaving the browser stuck. This is what happens when we run a very large loop and try to do other tasks in the meantime. It is not ideal when we think about user behavior. A user using your website will probably try to click on some links or probably hover over a link to get more info. They would like to do other stuff when javascript is waiting for a long operation to finish. How to solve this problem? That’s where callbacks come into the picture.

    Read More:  Design Patterns Overview: Helping You Write Better Software

    What is a Callback

    A callback is just a function call that runs after an asynchronous time taking expression has finished running. With the help of callbacks, javascript doesn’t have to keep watching if the expression is running or not. It will trust the callback to inform when it’s the execution is finished. With the callbacks keeping a check on an asynchronous call, JavaScript can perform other tasks in the meantime. It’s like the callback is telling JavaScript that “Hey, the expression has run. Can you please take care of me now?”

    You may have already used callbacks before in the setTimeout function. The first argument takes a function, and the second argument takes a number denoting the time in milliseconds. The function passed in the first arguments will be called only after a certain amount of time. We will refer to such a type of function as callbacks.

    setTimeout(function(){
        console.log("hey there!");
    }, 500);

    In the above code, the text “hey there” will be logged after 500 milliseconds.

    Do you remember where else we were using callbacks all this time? server.listen() and http.createServer() functions were taking a function as callbacks. So it turns out that you were already familiar with this term.

    Now the real question, we said that javascript could handle one thing at a time, then how does it can handle callbacks while doing other tasks at the same time? This is where Event Loop comes into play.

    What is Event Loop

    Let’s add one more expression to our add() example above.

    Event Loop example excallidraw
    Event Loop example excallidraw

    As you can see, we have introduced two more items in the image above. Event Loop and Task Queue

    Read More:  5 Website Security Threats and How to Counter Them

    Let’s see what happens when this piece of code runs.

    1. It will push the main() function into the call stack.
    2. Then it will push setTimeout() into the call stack.
    3. setTimeout is asynchronous. It will be popped from the call stack and pushed into the task queue. Task queue will hold all the callbacks to be called later.
    4. add() function is encountered and pushed to call stack.
    5. Nothing is left to process, so add() will return and get popped from the call stack.
    6. Now, only main() is left is a stack that will be popped out of the stack.
    7. Event Loop will see that the call stack is empty and there is one pending callback in the task queue. It the push the callback, which is just a function into the stack, and prints the log.
    8. Once the log is printed, it will be popped, and our function will finish running.

    Task Queue is just a callback queue that holds the callbacks until they are executed. Remember, it will only run when the call stack is empty. Event Loop ties the call stack and callback queue together. It will check when the call stack is empty and pushes the remaining callbacks into the stack. That how you can do other stuff like clicking when the fetch call is in process.

    The Event Loop is provided by the LibUV that we talked about earlier and it’s an essential concept that makes Nodejs so much efficient. Without the concept of Event Loop, Nodejs won’t be able to serve so many requests concurrently. Now that you have a clear understanding of how the whole Nodejs works internally, I hope you will be able to better explain it to others and write better code in general.

    I hope you understand how Event Loop runs and how callbacks help JavaScript run any asynchronous calls. That’s it for now, we will see you in the next lesson.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Mohammad Shad Mirza
    • X (Twitter)
    • LinkedIn

    JavaScript lover working on React Native and committed to simplifying code for beginners. Apart from coding and blogging, I like spending my time sketching or writing with a cup of tea.

    Related Posts

    Mastering REST APIs: Essential Techniques for Programmers

    December 18, 2024

    Crafting Interactive User Interfaces Using JavaScript Techniques

    December 17, 2024

    Effective Strategies for Utilizing Frameworks in Web Development

    December 16, 2024
    Leave A Reply Cancel Reply

    You must be logged in to post a comment.

    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo
    Don't Miss
    Interview May 5, 2017

    Interview with Ilya

    My name is Ilya / and I’m a professional JavaScript developer / with experience in both / front-end and back-end Areas. For over 3 years,/ I’ve been doing web development using popular frameworks / such as Angular, Meteor, Node.js and the other related technologies.

    Strategic LinkedIn Techniques for Real Estate Lead Generation

    December 16, 2024

    Interview with Alex

    June 29, 2017

    9. Уроки Node.js. События, EventEmitter и Утечки Памяти

    September 13, 2016

    Categories

    • AI & Automation
    • Angular
    • ASP.NET
    • AWS
    • B2B Leads
    • Beginners
    • Blogs
    • Business Growth
    • Case Studies
    • Comics
    • Consultation
    • Content & Leadership
    • CSS
    • Development
    • Django
    • E-commerce & Retail
    • Entrepreneurs
    • Entrepreneurship
    • Events
    • Express.js
    • Facebook Ads
    • Finance & Fintech
    • Flask
    • Flutter
    • Franchising
    • Funnel Strategy
    • Git
    • GraphQL
    • Home Services Marketing
    • Influencer & Community
    • Interview
    • Java
    • Java Spring
    • JavaScript
    • Job
    • Laravel
    • Lead Generation
    • Legal & Compliance
    • LinkedIn
    • Machine Learning
    • Marketing Trends
    • Medical Marketing
    • MSP Lead Generation
    • MSP Marketing
    • NestJS
    • Next.js
    • Node.js
    • Node.js Lessons
    • Paid Advertising
    • PHP
    • Podcasts
    • POS Tutorial
    • Programming
    • Programming
    • Python
    • React
    • React Lessons
    • React Native
    • React Native Lessons
    • Recruitment
    • Remote Job
    • SaaS & Tech
    • SEO & Analytics
    • Soshace
    • Startups
    • Swarm Intelligence
    • Tips
    • Trends
    • Vue
    • Wiki
    • WordPress
    Top Posts

    Роли внутри проектов

    Wiki March 18, 2016

    Interview with Alex

    Interview June 29, 2017

    Contentful+Gatsby = Smarter content management

    JavaScript September 2, 2019

    Unlock B2B Leads: Harnessing Strategic Partnerships Effectively

    B2B Leads November 24, 2024

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    About Us
    About Us

    Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

    7901 4th St N, Suite 28690
    Saint Petersburg, FL 33702-4305
    Phone: 1(877)SOSHACE

    Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
    Our Picks
    GraphQL

    Handling GraphQL API Authentication using Auth0 with Hasura Actions

    JavaScript

    Effective Strategies for Managing Project Dependencies

    Development

    Enhancing Software Performance: Strategies for Optimal Speed

    Most Popular

    Creating Mock API using Mirage in a React application

    React

    The Best Work Tools for Remote Teams — Part 1: Cloud & Productivity

    Job

    Performance Optimizations for React Native Applications

    JavaScript
    © 2025 Soshace Digital.
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions

    Type above and press Enter to search. Press Esc to cancel.