Hey friends! π Iβm Vikas Sankhla, a Full Stack Developer and founder of the Web Codder YouTube channel. Today, weβre diving into something super cool and powerful: Asynchronous JavaScript.
Sounds like a big word? Donβt worry! Iβll explain it like youβre my younger cousin learning coding for the first time. Letβs go step-by-step. π οΈ
π§© What Is Asynchronous Programming?
Imagine you’re making noodles π. While you’re waiting for the water to boil, you don’t just stand there, right? You might chop veggies or set the table.
Thatβs asynchronous in real life: doing other things while waiting!
In JavaScript, some tasks (like fetching data from the internet π) take time. Instead of stopping everything and waiting, JavaScript keeps doing other stuff.
This keeps your app fast and smooth.

π€ Why Is This Important?
If JavaScript was synchronous (meaning: one thing at a time), your app could freeze while waiting.
Imagine clicking a button and the whole page locks up for 5 seconds. π΅ Not fun, right?
Async helps to:
- π Speed up your app
- π Keep it responsive
- βοΈ Handle real-world stuff like:
- Loading data
- Waiting for user actions
- Animations & more
β²οΈ Using setTimeout
and setInterval
Letβs meet two superstars of async JavaScript:
1οΈβ£ setTimeout
This runs once after a delay.
Example:
jsCopyEditconsole.log("Hello!");
setTimeout(() => {
console.log("This shows after 2 seconds β³");
}, 2000);
console.log("Bye!");
What happens?
- Prints:
Hello!
- Then:
Bye!
- After 2 seconds:
This shows after 2 seconds β³
JavaScript doesnβt wait for setTimeout
. It keeps going!
2οΈβ£ setInterval
This runs again and again (like an alarm β°).
Example:
jsCopyEditsetInterval(() => {
console.log("This prints every 1 second β±οΈ");
}, 1000);
This keeps running until you stop it.

π Introduction to Callbacks
Callbacks are just functions inside functions.
Example: You tell your friend, βCall me π when dinner is ready.β
The phone call is the callback.
In JavaScript:
jsCopyEditfunction greet(name, callback) {
console.log("Hi " + name + "!");
callback();
}
function sayBye() {
console.log("Bye π");
}
greet("Alex", sayBye);
Output:
nginxCopyEditHi Alex!
Bye π
Simple, right? β
π΅ What Is Callback Hell?
Things get messy when you have many callbacks inside each other. It looks like this:
jsCopyEditdoSomething(function(result) {
doSomethingElse(result, function(newResult) {
yetAnotherThing(newResult, function(finalResult) {
console.log(finalResult);
});
});
});
This is hard to read and fix π«. People call it βCallback Hell.β
β¨ How Promises Help
Promises are like saying:
“Hey, I PROMISE Iβll do this task. Either I succeed (β
) or fail (β).”
Example:
jsCopyEditlet myPromise = new Promise((resolve, reject) => {
let success = true;
if (success) {
resolve("It worked! π");
} else {
reject("Oops, failed π’");
}
});
myPromise.then((message) => {
console.log(message);
}).catch((error) => {
console.log(error);
});

β
Using async
and await
(So Clean!)
async
and await
make async code look nice and clean, like normal code.
Example:
jsCopyEditasync function fetchData() {
console.log("Fetching data...");
let result = await new Promise((resolve) => {
setTimeout(() => resolve("Data is here! π¦"), 2000);
});
console.log(result);
}
fetchData();
Output:
Fetching data...
- (waits 2 seconds β³)
Data is here! π¦
So simple to read! π

π§ Key Concepts Recap
Concept | What It Does | Example |
---|---|---|
setTimeout | Runs once after a delay | Show alert after 3 seconds |
setInterval | Runs again and again at set intervals | Update clock every 1 second |
Callback | A function passed to another function | Call when button is clicked |
Promise | A task that will finish later (success/fail) | Get data from an API |
Async/Await | Cleaner way to write async code | Fetch user info from the server |
π― Conclusion
Async JavaScript is like juggling tasks πͺ β super useful when things take time. Youβve learned:
- Why async matters
- How to use
setTimeout
andsetInterval
- What callbacks are (and why callback hell is bad)
- How Promises and
async/await
save the day π
Want to keep leveling up your coding skills? π₯
π Subscribe to my YouTube for awesome tutorials: https://www.youtube.com/@web_codder/?sub_confirmation=1
π Check out automation tips: https://webcodder.dev/category/automation/
π Join our WhatsApp community: https://webcodder.dev/whatsapp
Letβs keep building amazing things together! πͺπ»β¨