How JavaScript Promises Work

How JavaScript Promises Work

JavaScript is single-threaded.

But it can handle:

  • API calls
  • Timers
  • File reads
  • Async operations

How?

You already learned about the Event Loop.
Now letโ€™s understand one of the most important tools that works with it:

Promises

๐Ÿ”น The Problem Before Promises

Before Promises, async code looked like this:

setTimeout(function() {
  console.log("Data loaded");
}, 1000);

Now imagine multiple async operations nested inside each other:

getData(function(data) {
  processData(data, function(result) {
    saveData(result, function() {
      console.log("Done");
    });
  });
});

This is called:

โŒ Callback Hell

Hard to read. Hard to maintain.

Promises solve this.


๐Ÿ”น What is a Promise?

In simple words:

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

Think of it like ordering food online.

  • You place the order
  • You wait
  • It either arrives (success)
  • Or gets cancelled (failure)

๐Ÿ”น Promise States

A Promise has 3 states:

  1. Pending โ€“ waiting
  2. Fulfilled โ€“ success
  3. Rejected โ€“ error

Once fulfilled or rejected, it is settled.


Example 1: Creating a Promise

const promise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve("Success!");
  }, 1000);
});
  • resolve() โ†’ success
  • reject() โ†’ failure

Example 2: Using .then()

promise.then((result) => {
  console.log(result);
});

Example 3: Handling Errors

const promise = new Promise((resolve, reject) => {
  setTimeout(() => {
    reject("Something went wrong!");
  }, 1000);
});

promise
  .then((result) => console.log(result))
  .catch((error) => console.log(error));

๐Ÿ”น Promise Chaining

Instead of nesting callbacks, we chain:

fetchData()
  .then(processData)
  .then(saveData)
  .then(() => console.log("Done"))
  .catch(console.error);

This keeps code clean and readable.


๐Ÿ”น How Promises Work with Event Loop

Important:

  • .then() callbacks go to the Microtask Queue
  • Microtasks run before setTimeout (macrotasks)

Example:

console.log("Start");

setTimeout(() => console.log("Timeout"), 0);

Promise.resolve().then(() => console.log("Promise"));

console.log("End");
Start
End
Promise
Timeout

Why?
Because Promise callbacks run before timers.

Event Loop


You know more about Event Loop Visit click on Learn More

Learn more

๐Ÿ”น Why Promises Are Important

Promises:

โœ” Avoid callback hell
โœ” Improve readability
โœ” Handle async flow better
โœ” Work perfectly with async/await
โœ” Are used everywhere (fetch, APIs, DB, etc.)


๐Ÿ”น Real-World Example

Whenever you use:

fetch("https://api.example.com")

It returns a Promise.

Thatโ€™s why you write:

fetch("https://api.example.com")
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));

๐Ÿ”น Conclusion

A Promise is simply:

A cleaner way to handle asynchronous operations in JavaScript.

It represents:

  • A future value
  • That might succeed
  • Or might fail

If you understand Promises, you understand modern JavaScript.