Skip to main content

Command Palette

Search for a command to run...

JavaScript Promises Explained for Beginners

Making Asynchronous Code Simple, Readable, and Powerful

Updated
3 min readView as Markdown
JavaScript Promises Explained for Beginners

Have you ever written code that depends on something that hasn't happened yet?

  • Data from an API

  • File loading

  • Timer Completion

Handling this used to be messy, until Promises came in.
In this blog we will understand Promises in the simplest way possible.

What Problems Do Promises Solve?

Before Promises, Developers used callbacks.

Callback Example:

setTimeout(() => {
  console.log("Step 1 done");

  setTimeout(() => {
    console.log("Step 2 done");

    setTimeout(() => {
      console.log("Step 3 done");
    }, 1000);

  }, 1000);

}, 1000);

Problem:

  • Hard to read

  • Hard to maintain

  • Nested structure (Callback hell)

Promises to the Rescue

A promise represents a value that will be available in the future.

Think of it like:

Ordering food online

  • You place an order --> Pending

  • Food delivered --> Fulfilled

  • Order Cancelled --> Rejected

Promise States

A promise has 3 states:

  • Pending --> Initial state

  • Fulfilled --> Operation successful

  • Rejected --> Operation failed

Promise Lifecycle (Visual)

Flow:

Pending → Fulfilled
        → Rejected

Basic Promise Example

const myPromise = new Promise((resolve, reject) => {
  let success = true;

  if (success) {
    resolve("Task completed!");
  } else {
    reject("Task failed!");
  }
});

Handling Success And Failure

myPromise
  .then(result => {
    console.log(result); // success
  })
  .catch(error => {
    console.log(error); // failure
  });

Key Idea:

  • .then() --> handles success

  • .catch() --> handles errors

Promise Chaining

Instead of nesting, we chain

fetch("https://api.example.com/user")
  .then(res => res.json())
  .then(user => {
    console.log(user);
    return user.id;
  })
  .then(id => {
    console.log("User ID:", id);
  })
  .catch(err => {
    console.log("Error:", err);
  });

Callbacks vs Promise

Feature Callbacks Promises
Readability Poor Clean and Structured
Nesting Deep (Callback hell) Flat chaining
Error Handling Difficult Centralized .catch()

Why Promises Improves Readability

Promises allows us to:

  • Write linear looking async code

  • Avoid deep nesting

  • Handle errors in one place

Everyday Analogy

Courier Delivery

  • You order a package

  • You don't wait at the warehouse

  • You continue your work

  • Package arrives later

That's exactly how promises work.

Suggestion For Learning Promises

1. Think in Future values

Always ask:

"This value will come later, how will I handle it?"

2. Avoid Callback hell

Refactor nested callbacks into promises

3. Practice Chaining

Break big tasks into smaller .then() steps.

4. Handle errors properly

Always use .catch() to avoid silent failures

5. Move to async/await (Next step)

Promises become even cleaner with async/await.

Final Thoughts

  • Promises represents future result

  • They solve callback complexity

  • They make async code:

    • Cleaner

    • More readable

    • Easier to manage