DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Clean Up Event Data in Ansible Event-Driven Automation
  • An Introduction to Object Mutation in JavaScript

Trending

  • Creating a Web Project: Caching for Performance Optimization
  • How to Build Real-Time BI Systems: Architecture, Code, and Best Practices
  • Agile’s Quarter-Century Crisis
  • How to Merge HTML Documents in Java
  1. DZone
  2. Data Engineering
  3. Data
  4. Event Loop in JavaScript

Event Loop in JavaScript

JavaScript is single-threaded, meaning only one action can be performed at a time. Event Loop is the queue of commands to be called.

By 
Sebastian Z. user avatar
Sebastian Z.
·
Aug. 08, 22 · Presentation
Likes (2)
Comment
Save
Tweet
Share
5.8K Views

Join the DZone community and get the full member experience.

Join For Free

JavaScript is an integral part of any website. It is responsible for all its logic and data manipulation. It is the most popular language used to build websites. This extremely versatile language can be used for virtually anything. This does not necessarily mean that it will best suit all applications, but it will be able to compete with the leading languages in many respects. It is this versatility that makes it worth finding out about its underlying mechanisms and thus its strengths and drawbacks. 

JavaScript is single-threaded, which means that only one action can be performed at a time. JavaScript uses a single thread to handle all requests, but when creating contexts, it uses other threads to handle input/output, so it does not get stuck. JavaScript can communicate between threads, using external interfaces that are able to create such other threads, it can communicate. For example, if you create a web page, it runs on a single thread but is able to communicate with other threads that work for some specific purpose. This is the case when using a camera. The camera is such a separate service for the browser and is a separate thread. And this is not happening on our thread but on an external one. 

Let us explore the structures that go into the event loop.

 What Is a Stack/Queue?

  • A stack is a data structure in which things that came last are executed first. 
  • Queue – the thing that comes first will be executed first. 
  • Web APIs – additional interfaces provided by the browser. They run on separate browser threads. These include methods related to timers (e.g., setTimeout), sending XHR requests (XMLHttpRequest class), or manipulating and responding to events from the DOM tree.

A Web API from subsequent data structures that decide which elements are more important to execute in the browser. We have the main thread and the side threads, but at some point, we have to decide when to transfer the result to the main thread. And here we can decide if there are two things waiting for us at the same time, which of these things is more important.

We divide the queues into macro and micro tasks and a rendering queue. They contain function calls indicated as callbacks to asynchronous methods from the Web API. (setTimeout, setInterval, xhr, querySelector). The event loop executes tasks as long as it has such tasks to perform. First the micro, then the macro ones. In the case of rendering, the event loop processes what is in the queue and nothing else. If something comes along, it has to wait. 

A stack.

A queue. 


Synchronous/Asynchronous Code

  • Synchronous: executed line by line. What is specific here is that we can only execute one piece of code at a time. Once we start it, we are unable to do anything until it is done.
  • Asynchronous: can be executed in parallel to the rest of the application without disturbing it, but also the result of the action is not immediate. Example: querying the API and animating at the same time.

The three main elements of the event loop. EL is the queue of commands to be called. And it does not matter if they are synchronous or asynchronous, these are all actions that the user has performed and must be processed. They wait in a queue, each is taken in turn, in the order from which they came, and when they are collected, the decision is executed. If synchronous, it is executed at that time, the rest waits, and it goes into JavaScript. If asynchronous, its indicator is written to the memory chip, and its execution itself is delegated to the web APIs. The event loop forgets this; it just passes the message that this is asynchronous code and "do it for me, just give me the result." The event loop continually fetches things from the queue one by one. Things that are delegated to the web API are queued anyway, and this way, everything is done continuously, i.e., in a "loop." 

Event Loop

What Is the Event Loop?

JavaScript is a programming language that makes it possible to implement complex elements on a website so that the website can not only display static information but also handle content changes as appropriate, display interactive maps and animations of 2D/3D graphics, display video, and so on. Thanks to JS, you can take dynamic content creation on a website, control multimedia, animate images, and almost everything else.

The event loop determines the order in which the code will run. It brings the synchronous and asynchronous worlds together. If we do something in turn, we have access to it. If we delegate something, we would like to be able to keep the value from it later. For example, make a query to the server to retrieve data – not only do we want the data to be retrieved, but we also want to receive it. And since we have gone somewhere further with our code, we need to have some mechanism in place to pick up that data in the future.

To understand what an event loop is and to learn all its mechanisms, we need to distinguish between the two concepts, which are the synchronous and asynchronous code. The synchronous code is the one that, when called, calls itself line by line, has no side effects and will be executed from A to Z, exactly what was called. The asynchronous code, on the other hand, is a code that, at certain stages of the call, part of the code is delegated to be called in a separate thread or at another time. For example, we want to create an animation and download data from the server at the same time. If we did this in the synchronous code, we would either have to wait for the data or show an animation. In the asynchronous code, we can alternate tasks at the same time. We will not notice this because it will be managed by the browser. 

Event loop JavaScript Data (computing) Event

Published at DZone with permission of Sebastian Z.. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Clean Up Event Data in Ansible Event-Driven Automation
  • An Introduction to Object Mutation in JavaScript

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!