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

Alok SinghAlok Singh
3 min read

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?

ApproachLooks LikeBest For
CallbacksDesi Jugaad πŸ› οΈSmall async tasks
PromisesSwag Wala Waada 🀝Cleaner handling of results
.then()Step by Step πŸͺœChaining dependent tasks
async/awaitSeedha 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"

0
Subscribe to my newsletter

Read articles from Alok Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Alok Singh
Alok Singh