Callback Functions, Promises, .then(), and async/await β Desi Style π»

If you're a developer learning JavaScript, you might feel like your brain gets fried dealing with asynchronous code. But donβt worry β letβs break it down using desi analogies we all know and love.
1. Callback Functions β "Maa bula rahi hai!" π©βπ¦π²
A callback function is like when your mom says:
"Beta, chai bana lo. Jab ho jaye, mujhe bula lena."
You go to the kitchen to make chai (chai = some task). Once it's ready, you call your mom (callback), and she comes to drink it.
In code:
javascriptCopyEditfunction makeChai(callback) {
console.log("Chai ban rahi hai...");
setTimeout(() => {
console.log("Chai ban gayi!");
callback();
}, 2000);
}
makeChai(() => {
console.log("Maa aa gayi chai peene. β");
});
Callback: You passed a function to another function and told it to run after the chai is ready.
Problem?
What if you had to make chai, serve biscuits, and bring a newspaper one after another? Callback hell! Nested pyramids of doom. π΅
2. Promises β βMujhe waada do!β π€
A Promise is like your friend saying:
"Tu bas order de. Pizza 30 minutes mein aa jayega, warna free!"
Thatβs a promise β it may be fulfilled (pizza aa gaya π) or rejected (pizza nahi aaya β).
In code:
javascriptCopyEditconst pizzaPromise = new Promise((resolve, reject) => {
let pizzaReady = true;
if (pizzaReady) {
resolve("Pizza mil gaya! π");
} else {
reject("Pizza nahi aaya. π€");
}
});
pizzaPromise.then((msg) => {
console.log("Yay! " + msg);
}).catch((err) => {
console.log("Oops! " + err);
});
.then()
runs when the promise is fulfilled.catch()
runs when itβs rejected
.then()
β "Kaam ho gaya, ab agla step..." π
You can chain .then()
For sequential tasks:
javascriptCopyEditorderPizza()
.then((pizza) => eatPizza(pizza))
.then(() => washHands())
.then(() => nap())
.catch((err) => console.log("Kuch gadbad hai: " + err));
Desi Analogy:
"Jab pizza aayega, khayenge.
Jab khayenge, haath dhoyenge.
Jab haath dhoyenge, so jayenge." π΄
4. Async/Await β "Aram se, step by step." πβπ
This is the most shudh and readable way to write async code.
Imagine your mom is saying:
"Beta, pehle chai bana. Phir safai kar. Fir mujhe bazaar le chal."
In code:
javascriptCopyEditasync function dailyRoutine() {
try {
const chai = await makeChai();
console.log(chai);
const cleaned = await cleanRoom();
console.log(cleaned);
const marketTrip = await goToMarket();
console.log(marketTrip);
} catch (err) {
console.log("Kuch to gadbad hai: " + err);
}
}
Here, you wait for each task before moving to the next one. No .then()
, no pyramid, just clean code.
Bonus: Which One Should You Use?
Approach | Looks Like | Best For |
Callbacks | Desi Jugaad π οΈ | Small async tasks |
Promises | Swag Wala Waada π€ | Cleaner handling of results |
.then() | Step by Step πͺ | Chaining dependent tasks |
async/await | Seedha Sadha Desi Code π§ | Readable and manageable logic |
Summary β Desi Style π
Callback = "Ho gaya to mujhe bata dena"
Promise = "Tera kaam pakka, ya to hoga ya mana karenge"
.then()
= "Ho gaya? Agla kaam shuru"async/await
= "Ek kaam khatam, fir doosra"
Subscribe to my newsletter
Read articles from Alok Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
