Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Asynchronous JavaScript

JavaScript is single-threaded — it can execute only one task at a time.
But thanks to its asynchronous nature, it can still perform multiple operations efficiently without freezing or blocking your app.

This section helps you understand how JavaScript achieves multitasking using concepts like the Event Loop, Callbacks, Promises, and Async/Await.


What Does “Single-Threaded” Mean?

A thread is like a path where your code runs.
Since JavaScript has only one thread, it can handle only one task at a time on the main thread.

Imagine you’re the only cook in a kitchen — you can make one dish at a time,
but you can still manage many orders if you use your time wisely!


Why Asynchronous Code?

Without async behavior, JavaScript would wait for every task (like a slow API or timer) before moving on.
This would make your web apps freeze whenever they’re waiting for something.

Async operations allow your app to:

  • Stay responsive
  • Handle multiple tasks in the background
  • Avoid blocking other code

How JavaScript Handles Async Code

Even though JS is single-threaded, it has a smart system made of:

  • Call Stack → Runs one task at a time
  • Web APIs / Node APIs → Handle async operations (like setTimeout, fetch)
  • Callback Queue → Stores finished async tasks
  • Event Loop → Moves tasks from the queue to the stack when it’s free

Methods to Handle Asynchronous Code

JavaScript provides several ways to manage asynchronous operations.
Over time, these methods have evolved to make async programming easier to understand and maintain.

These include:

1. Callbacks (Traditional Method)

The earliest way to handle async tasks by passing a function to be called later.


2. Promises

A cleaner and more reliable way to handle asynchronous operations, avoiding callback hell.


3. Async / Await

Built on top of Promises, allowing async code to look and behave like synchronous code.


4. Event Listeners (Browser-Specific Async Behavior)

Used in browsers to handle user interactions like clicks or key presses asynchronously.


5. Fetch API

A modern way to make network requests (like getting data from an API) asynchronously.


6. Event Loop (The Core Mechanism)

Not a function, but the heart of how async JavaScript runs.
It continuously checks if the call stack is empty and pushes queued async tasks back to execute.