🚀 Understanding JavaScript Promises
Image Credit: CodeBurst

🚀 Understanding JavaScript Promises

Ever ordered juice online? 🍹 You click “Order Now” and wait.

That’s exactly how JavaScript Promises work. They’re a way to handle things that take time, like loading data from a server, waiting for a user’s response, or running any async code.


🧠 Let’s break it down:

A Promise is a special object in JavaScript that has three possible states:

  1. Pending – The promise is still waiting (like your juice is being prepared)
  2. Fulfilled – It worked! You got your juice.
  3. Rejected – Something went wrong. No juice for you.


💡 Real-life example: Juice Order App

const juiceOrder = new Promise((resolve, reject) => {
  const inStock = true;

  if (inStock) {
    resolve("Juice is ready!");
  } else {
    reject("Sorry, no juice today.");
  }
});

juiceOrder
  .then((message) => console.log(message))  // Runs if promise is fulfilled
  .catch((error) => console.log(error));    // Runs if promise is rejected        

✅ If inStock is true → you get: "Juice is ready!"

❌ If inStock is false → you get: "Sorry, no juice today."


🧩 Why are Promises useful?

Because JavaScript doesn’t like to wait. It keeps doing other things while waiting for data (non-blocking).

With promises, you can tell JS:

“Hey, once the data comes in, do this. If it fails, do that.”

🔄 Promise Lifecycle in 5 Secs:

  1. You create a promise.
  2. You do something async (like an API call).
  3. If it works → resolve
  4. If it fails → reject
  5. You use .then() to handle success, and .catch() for errors.


✨ Clean Syntax with async/await

You can use async/await to write promises like they’re normal code:

async function getJuice() {
  try {
    const message = await juiceOrder;
    console.log(message);
  } catch (error) {
    console.log(error);
  }
}        

More readable. Less .then() chains. Still works the same under the hood.


🚀 Bonus: Promise Methods

Here are some useful tools to work with multiple promises:

  • Promise.all() – Wait for all to finish
  • Promise.race() – Use the first one that finishes
  • Promise.allSettled() – Wait for all, even if some fail
  • Promise.any() – Use the first successful one


✅ What’s a Promise?

A Promise is a way to say: “I don’t have the result yet, but I will. Either with success or an error.”

Next time you’re waiting for something in your app, think: "You just placed an order for juice." 🍹

Let JavaScript handle the waiting for you.


👉 Hope this helped simplify Promises for you! If it did, leave a 👍 or share with someone who’s just starting out with JavaScript.

#JavaScript #WebDevelopment #Programming #Async #Promises #CodingTips #LearnToCode #javas

VIGNESH KUMAR

Attended Anna University, Chennai

3mo

Awesome post, Manish! Your JavaScript tips are spot-on and super helpful for fellow web dev learners. At Akadmex (https://akadmex.com), we’re empowering creators like you to turn your knowledge into 15-min educational videos — and earn for each one. Ready to turn your coding skills into impact? Join us at akadmex.com! #Akadmex #JavaScript #WebDevelopment #MicroLearning #StudentCreators #Tier2and3Talent #CodeAndTeach

Like
Reply
Aditya Wakale

Software Engineer @Evora IT Solutions | SAP MDK | JavaScript & TypeScript Enthusiast | Passionate About Scalable UI

3mo

Well put, Definitely worth reading ✌️

Ethan Wallace-Arellano

Footballer & Father | Co-Founder & Co-CEO of FootIntel | Explosive Left-Footed Winger/Wingback | Bachelor's Degree in Sport Psychology

3mo

Great stuff Manish 🤌🤌

To view or add a comment, sign in

Others also viewed

Explore topics