Skip to content

Commit f04e706

Browse files
committed
Added README file explaining how JS handles asynchronous operations
1 parent 590b107 commit f04e706

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

async-methods/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Asynchronous JavaScript
2+
3+
JavaScript is **single-threaded** — it can execute **only one task at a time**.
4+
But thanks to its **asynchronous nature**, it can still perform multiple operations efficiently without freezing or blocking your app.
5+
6+
This section helps you understand **how JavaScript achieves multitasking** using concepts like the **Event Loop**, **Callbacks**, **Promises**, and **Async/Await**.
7+
8+
---
9+
10+
## What Does “Single-Threaded” Mean?
11+
12+
A thread is like a path where your code runs.
13+
Since JavaScript has **only one thread**, it can handle **only one task at a time** on the **main thread**.
14+
15+
> Imagine you’re the only cook in a kitchen — you can make one dish at a time,
16+
> but you can still manage many orders if you use your time wisely!
17+
18+
---
19+
20+
## Why Asynchronous Code?
21+
22+
Without async behavior, JavaScript would wait for every task (like a slow API or timer) before moving on.
23+
This would make your web apps **freeze** whenever they’re waiting for something.
24+
25+
Async operations allow your app to:
26+
- Stay **responsive**
27+
- Handle multiple tasks in the background
28+
- Avoid blocking other code
29+
30+
---
31+
32+
## How JavaScript Handles Async Code
33+
34+
Even though JS is single-threaded, it has a smart system made of:
35+
36+
- **Call Stack** → Runs one task at a time
37+
- **Web APIs / Node APIs** → Handle async operations (like `setTimeout`, `fetch`)
38+
- **Callback Queue** → Stores finished async tasks
39+
- **Event Loop** → Moves tasks from the queue to the stack when it’s free
40+
41+
---
42+
43+
## Methods to Handle Asynchronous Code
44+
45+
JavaScript provides several ways to manage asynchronous operations.
46+
Over time, these methods have evolved to make async programming easier to understand and maintain.
47+
48+
These include:
49+
50+
### 1. **Callbacks (Traditional Method)**
51+
The earliest way to handle async tasks by passing a function to be called later.
52+
53+
---
54+
55+
### 2. **Promises**
56+
A cleaner and more reliable way to handle asynchronous operations, avoiding callback hell.
57+
58+
---
59+
60+
### 3. **Async / Await**
61+
Built on top of Promises, allowing async code to look and behave like synchronous code.
62+
63+
---
64+
65+
### 4. **Event Listeners (Browser-Specific Async Behavior)**
66+
Used in browsers to handle user interactions like clicks or key presses asynchronously.
67+
68+
---
69+
70+
### 5. **Fetch API**
71+
A modern way to make network requests (like getting data from an API) asynchronously.
72+
73+
---
74+
75+
### 6. **Event Loop (The Core Mechanism)**
76+
Not a function, but the **heart of how async JavaScript runs**.
77+
It continuously checks if the call stack is empty and pushes queued async tasks back to execute.
78+
79+
---

0 commit comments

Comments
 (0)