Understanding JavaScript Promises Through Pizza Delivery πŸ•

Abhishek TiwariAbhishek Tiwari
3 min read

πŸ“š Introduction: Why Do We Need Promises?

JavaScript is a single-threaded language β€” it handles one task at a time. But in modern apps, we often need to handle asynchronous operations, like:

  • Fetching data from a server

  • Reading files

  • Waiting for timeouts

Earlier, we used callbacks, but they often led to messy, nested code β€” a situation known as callback hell 😩.

That’s where Promises come in β€” a cleaner way to handle async tasks in JavaScript.


πŸ• Real-Life Analogy: Ordering a Pizza

Let’s say you ordered a pizza. You’re not just going to sit and stare at the oven β€” you’ll probably watch Netflix or scroll through your phone.

Now the pizza can either:

  1. Be delivered successfully βœ…

  2. Get canceled ❌

  3. Still be in progress ⏳

That’s exactly how a Promise works β€” it handles a value that may be available now, later, or never.


πŸ§‘β€πŸ³ Let’s Write a Promise Function

function orderPizza() {
  return new Promise((resolve, reject) => {
    let pizzaReady = true;

    setTimeout(() => {
      if (pizzaReady) {
        resolve("Yay! Pizza is here πŸ•");
      } else {
        reject("Oops! Pizza got canceled πŸ˜”");
      }
    }, 2000);
  });
}

orderPizza()
  .then((message) => {
    console.log("Success:", message);
  })
  .catch((error) => {
    console.log("Error:", error);
  });

πŸ” What’s Happening Here?

  • The function orderPizza() returns a Promise.

  • After a 2-second delay, it checks whether the pizza is ready.

  • If yes β†’ it calls resolve() (promise is fulfilled).

  • If not β†’ it calls reject() (promise is rejected).

  • .then() handles success.

  • .catch() handles errors.


πŸ€” Why Use Promises?

  • Avoids callback hell

  • Makes async code easier to read and maintain

  • Allows chaining of multiple async steps


πŸ”— Example: Promise Chaining (Bake β†’ Deliver)

function bakePizza() {
  return new Promise((resolve) => {
    setTimeout(() => resolve("Pizza baked πŸ•πŸ”₯"), 1000);
  });
}

function deliverPizza() {
  return new Promise((resolve) => {
    setTimeout(() => resolve("Pizza delivered πŸ›΅"), 1000);
  });
}

bakePizza()
  .then((msg1) => {
    console.log(msg1);
    return deliverPizza();
  })
  .then((msg2) => {
    console.log(msg2);
  })
  .catch((err) => {
    console.log("Something went wrong:", err);
  });

πŸ–ΌοΈ Visual Recap:

  • Left: User places a pizza order

  • Middle: Pizza is being prepared (Pending)

  • Right: Either delivered (Fulfilled) or canceled (Rejected)


πŸš€ Quick Summary

  • Promises represent the result of an asynchronous operation.

  • They can be in 3 states: Pending, Fulfilled, or Rejected.

  • Use .then() to handle success, and .catch() for errors.

  • Real-world analogy: Pizza ordering makes it fun to learn πŸ•


If you enjoyed this blog, give it a like ❀️, leave a comment πŸ“, and follow for more real-life JavaScript explanations!

1
Subscribe to my newsletter

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

Written by

Abhishek Tiwari
Abhishek Tiwari