Skip to main content

Command Palette

Search for a command to run...

JavaScript Promises Explained (Without Getting Confused)

Updated
•2 min read•View as Markdown
K

I write technical articles for developers who already know the basics but want clarity. No hand-holding, no fluff — just structured explanations for people revisiting concepts or fixing confusion.

At some point in JavaScript, things don’t happen instantly.

Like:

  • fetching data from an API

  • reading a file

  • waiting for a response

This creates a problem… how do we handle something that will complete later?

That’s where Promises come in.


What problem promises solve

Before promises, we used callbacks.

Example:

getData(function(result) {
  process(result, function(final) {
    console.log(final);
  });
});

This gets messy very fast (callback hell 😭).

Promises solve this by making code:

  • cleaner

  • easier to read

  • easier to manage


What is a Promise (simple idea)

A Promise is a value that will be available in the future.

Like:

“I promise I’ll give you the result later.”


Promise states

A promise has 3 states:

  • pending → still working

  • fulfilled → success

  • rejected → failed

Once it’s fulfilled or rejected, it doesn’t change again.


Basic promise lifecycle

Example:

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

  if (success) {
    resolve("Data received");
  } else {
    reject("Error occurred");
  }
});

Here:

  • resolve → success

  • reject → failure


Handling success and failure

We use .then() and .catch():

promise
  .then((data) => {
    console.log(data);
  })
  .catch((err) => {
    console.log(err);
  });
  • .then() → handles success

  • .catch() → handles errors


Promise chaining concept

One of the best parts of promises is chaining.

fetchData()
  .then((data) => processData(data))
  .then((result) => console.log(result))
  .catch((err) => console.log(err));

Instead of nesting, everything flows step by step.

Much cleaner than callbacks.


Promises vs callbacks (quick idea)

  • Callbacks → messy when nested

  • Promises → readable flow

Promises make async code feel more structured.


Final thought

Promises are just about handling future values in a clean way.

Once you understand this, async JavaScript becomes way less confusing.