Logo

dev-resources.site

for different kinds of informations.

JavaScript Event Loop

Published at
8/27/2024
Categories
javascript
eventloop
asynchronous
Author
lakmal_asela_8be4eb30d9db
Categories
3 categories in total
javascript
open
eventloop
open
asynchronous
open
Author
25 person written this
lakmal_asela_8be4eb30d9db
open
JavaScript Event Loop

What is the JavaScript Event Loop?

In JavaScript, an event loop is a mechanism that controls the execution of code, events, or messages using non-blocking I/O. This provides a way for non-blocking, or asynchronous, operations in JavaScript.

Key Concepts

1.Single Threaded
JavaScript is single-threaded, which means it executes one task at a time. Single threaded, therefore, one thread on which JavaScript executes is the so-called "main thread.

2.Call Stack
It is the data structure in which JavaScript keeps track of function calls. A function call is pushed onto the stack. When it returns, it is removed. When it is empty, JavaScript is ready to process the next thing. It is also commonly referred to as the "main thread".

3.Heap
This is where JavaScript stores objects and variables. It's used for dynamic memory allocation.

4.Event Queue
A queue of messages or tasks that are waiting to get executed. When a task is added into the queue, it waits for the call stack to be empty to execute.

5.Event Loop
It is something that constantly monitors the call stack and event queue. If the call stack is empty, it then moves tasks from the event queue into the call stack and executes them.

Image description

Process

  • Execution of Code: When JavaScript starts to execute code, it pushes the function calls onto the call stack. Additionally, it executes one function after another.
  • Asynchronous Operations: Immediately an operation is asynchronous, like a setTimeout or a network request, the JavaScript does not block the execution. Instead, it forwards that operation to Web APIs, say to the browser's timer or to services handling HTTP requests.
  • Callback Functions: When an asynchronous operation completes, its callback function is pushed into the event queue.

  • Event Loop Checking: Event Loop now checks the call stack as well as the event queue in the order. If the call stack is empty, it picks the first task from the event queue and pushes that into the call stack to run it.

console.log('Start');

setTimeout(() => {
  console.log('Timeout 1');
}, 1000);

setTimeout(() => {
  console.log('Timeout 2');
}, 500);

console.log('End');

Enter fullscreen mode Exit fullscreen mode

Note that the following will occur step-by-step:

  • Since start is a synchronous operation, it's logged immediately.
  • The first setTimeout is registered with 1000ms delay and then goes to the Web APIs. Its callback will be put in the event queue after 1000ms.
  • The second setTimeout is registered with 500ms delay and then goes to the Web APIs. Its callback will be put in the event queue after 500ms.
  • End is loged right away because it's synchronous.
  • In 500ms, the callback for the second setTimeout moves from the event queue to the call stack and logs Timeout 2.
  • The first rate for setTimeout undergoes from the event queue to the call stack in 1000ms and log Timeout 1.

Summary

  • Call Stack: Executes functions in order.
  • Event Queue: Stores messages or tasks to be executed.
  • Event Loop: It executes the tasks, which are passed from the event queue into the call stack when it's empty.
asynchronous Article's
30 articles in total
Favicon
Why is My Multi-Threaded API Still Slow?
Favicon
Building Async APIs in ASP.NET Core - The Right Way
Favicon
Understanding Synchronous and Asynchronous Bus Timing
Favicon
Async Vs Sync, which is most preferrable?
Favicon
Mastering Asynchronous Programming in JavaScript
Favicon
Why Modern Programming Isn't Always Asynchronous (And That's Okay, Mostly)
Favicon
Zero-Cost Abstractions in Rust: Asynchronous Programming Without Breaking a Sweat
Favicon
Why Wait? Exploring Asynchronous and Non-Blocking Programming 🚦
Favicon
The Tale of Async, Multithread, and Parallel: Heroes of the .NET Realm
Favicon
Getting Started with Asynchronous Programming in Dart
Favicon
JavaScript Event Loop
Favicon
Asynchronous programming in Javascript
Favicon
This Week I Learnt: Java's CompletableFuture
Favicon
How to Run a Method Asynchronously in a Reactive Chain in Spring WebFlux?
Favicon
Programación asincrónica en Javascript
Favicon
Understanding the Event Loop, Callback Queue, and Call Stack & Micro Task Queue in JavaScript
Favicon
Mastering Callbacks in JavaScript 💻🔥
Favicon
Mastering Concurrency in C with Pthreads: A Comprehensive Guide
Favicon
Async/Await keeps order in JavaScript;
Favicon
Rate my python async snippet
Favicon
Returning a Promise — How an async function operates
Favicon
A Project of async springboot and react js from where i can learn please help...
Favicon
GPT teaches me how to make my logic sync and async at the same time with trampolines
Favicon
Understanding JavaScript Promises and Callbacks
Favicon
Pros and Cons of Event-Driven Architecture
Favicon
Asynchronous Communication with Apache Kafka
Favicon
Dig deeper into JavaScript.
Favicon
Is async/await a good idea? 🤔 async/await vs promises
Favicon
What is RabbitMQ and why do you need a Message Broker?
Favicon
🍳 Asynchronous Programming in C# Made Easy: Breakfast Included! ☕️

Featured ones: