Skip to main content

Command Palette

Search for a command to run...

Promise — Dattebayo!

Updated
•8 min read•View as Markdown

What is a Promise?

Think about Naruto. He was a small kid in Konoha. Nobody liked him. Nobody was his friend. The whole village ignored him. He had no parents. He used to sit alone on a swing outside his school.

One day, he looked at the big mountain where the Hokage faces are carved. And he said one thing

"I will become Hokage one day. Believe it."

Nobody laughed. Nobody believed him. Even his teacher Iruka was not sure. Naruto himself did not know how he will do it. He did not know when. But he made a commitment. He said — someday, this will happen. I am promising it right now.

That is a Promise.

In JavaScript, a Promise is the same thing.

It is an object. This object says "I do not have the answer right now. But I am working on it. When I finish, I will either succeed or fail. But I will always tell you what happened."

const promiseToBecomeHokage = new Promise((resolve, reject) => {
  const narutoTrained = true;
  if (narutoTrained) {
    resolve("Naruto is now the Seventh Hokage!");
  } else {
    reject("The dream could not be fulfilled...");
  }
});

Every Promise has three states. Think of them like this

Pending :- Naruto is still training. He is still fighting. The answer has not come yet. This is the waiting time between the promise and the result.

Fulfilled :-Naruto did it. He became Hokage. This means resolve ran. The promise succeeded.

Rejected :- Something went wrong. The mission failed. This means reject ran. The promise failed.

One important rule :- a Promise always starts as pending. And it always ends as either fulfilled or rejected. It can never stay in the middle forever. It will always reach one final answer.

How Do We Handle a Promise?

Naruto is in the middle of a big fight. He is losing. He needs more power. So he goes inside himself into that dark place where Kurama lives and he asks for chakra.

That request is the Promise. Naruto asked. Now he is waiting. The answer has not come yet. This is pending.

Now two things can happen. Kurama can say yes. Or Kurama can say no. Based on that, we handle the result. In JavaScript we handle a Promise using three things .then(), .catch(), and .finally().

.then() — When the Promise is Fulfilled

Kurama looks at Naruto. He sees how hard this kid has fought. He sees the pain and the will. And he says

"Fine. Take my chakra."

The Promise resolved. Things went well. Now .then() runs. It takes the good result and does something with it.

promiseToGetChakra
  .then(result => {
    console.log(result);
    // "Kurama gave his chakra. Naruto is now unstoppable."
  });

.catch() — When the Promise is Rejected

But what if Kurama had said no? What if he had turned away and said —

"I will never help you. You are on your own."

The Promise rejected. Something went wrong. Now .catch() runs. It takes the failure and handles it.

promiseToGetChakra
  .then(result => {
    console.log(result);
  })
  .catch(error => {
    console.log(error);
    // "Kurama refused. Naruto has to fight alone."
  });

But What Happens When There Are Multiple Promises?

Naruto did not make just one promise in his life. He promised to bring Sasuke back. He promised to protect the village. He promised to never leave his friends. Many promises, all running at the same time, each with its own result.

JavaScript gives us three tools for this Promise.all, Promise.allSettled, and Promise.any. Each one handles multiple promises in a different way. And each one has a Naruto character living inside it.

Promise.all — Neji's Way

You know Neji Hyuga. He believed in fate. He had one rule for everything all or nothing.

Now think like this. The Hokage gives Team Guy three missions to run at the same time. Neji says "Either all three missions succeed, or this whole operation is a failure. If even one mission fails, I am reporting it as failed. I do not care about the other two."

That is Promise.all. It runs all promises at the same time. It waits for every single one to resolve. Only then it gives you the results. But if even one promise rejects — it stops everything immediately and goes straight to .catch(). It does not wait for the others. It does not care what the others did. One failure and it is over.

const mission1 = Promise.resolve("Scroll retrieved ");
const mission2 = Promise.resolve("Enemy defeated ");
const mission3 = Promise.reject("Ambushed... ");

Promise.all([mission1, mission2, mission3])
  .then(results => {
    console.log("All missions done:", results);
    // Never reaches here
  })
  .catch(err => {
    console.log("Operation failed:", err);
    // "Ambushed... "
    // Neji bows his head. Fate decided.
  });

Mission 1 was done. Mission 2 was done. It did not matter. Mission 3 failed and Neji shut everything down.

Use Promise.all when you need every single result and one failure means the whole thing should stop. Like loading parts of a page that all depend on each other. If one part is missing, nothing works anyway.

Promise.allSettled — Naruto's Way

Now give those same three missions to Naruto.

He runs. He fights. He gets hurt. Mission 3 goes wrong and he takes a big hit. But Naruto does not fall down. He does not throw away the two good results because of one failure. He comes back to the village and gives a full, clear report

"Mission 1 done. Mission 2 done. Mission 3 failed, and here is why. I am not hiding anything. Tell me what to do next."

That is Promise.allSettled. It waits for every promise to finish success or failure. Then it gives you every single result. It never rejects. It always resolves. You always get the full picture.

const mission1 = Promise.resolve("Scroll retrieved ");
const mission2 = Promise.resolve("Enemy defeated ");
const mission3 = Promise.reject("Ambushed... ");

Promise.allSettled([mission1, mission2, mission3])
  .then(results => {
    results.forEach(result => {
      if (result.status === "fulfilled") {
        console.log("Success:", result.value);
      } else {
        console.log("Failed:", result.reason);
      }
    });
  });

// Success: Scroll retrieved 
// Success: Enemy defeated 
// Failed: Ambushed... 

Notice there is no .catch() here. That is because Promise.allSettled never throws. It sits with every result the good ones and the bad ones just like Naruto always did.

Use Promise.allSettled when partial success is still useful and you want to know exactly what happened to each promise. Like a dashboard that should show whatever data it can, and show an error for whatever it could not get. You do not shut down the whole page because one thing failed.

Promise.any — Hinata's Way

Hinata loved Naruto from the academy days. For years nobody knew. She trained harder just to walk beside him. She sent him so many signals. He never understood.

She did not need the whole world to say yes. She did not need every mission to work out. She did not need all promises to resolve.

She just needed one. One moment. One yes. One single promise to come through.

And when that day came when Naruto finally understood her feelings Hinata's whole world became complete. She did not wait for anyone else. She did not care about the rejections that came before. One promise resolved and that was enough.

That is Promise.any. You give it many promises and it gives you back the first one that succeeds. It ignores all the rejections as long as at least one promise comes through. It only truly fails and throws an AggregateError if every single promise rejects.

const confession1 = Promise.reject("He didn't notice");
const confession2 = Promise.reject("Wrong moment");
const confession3 = Promise.resolve("He finally understood ");

Promise.any([confession1, confession2, confession3])
  .then(result => {
    console.log("It happened:", result);
    // "He finally understood "
    // Hinata smiles
  })
  .catch(err => {
    console.log("Every single one failed:", err);
    // AggregateError — only if ALL reject
  });

The first two rejected. Hinata did not break. Because the third one came through. And that was enough.

Use Promise.any when you have many fallbacks and you only need the first one that works. Like trying three different servers. You do not care which one responds. You just need one to show up.

The Final Summary

Method Character Resolves When Rejects When
Promise.all Neji Every promise succeeds Even one fails
Promise.allSettled Naruto Always — after all settle Never
Promise.any Hinata Even one succeeds Every single one fails

Neji needed everything to go right. If even one thing went wrong, he called the whole thing meaningless.

Naruto took every result the wins and the failures both. He sat with all of it. He did not break. He kept going.

Hinata just needed one promise to come through. Only one. And when it did, it was everything for her.

Three tools. Three different ways to handle multiple promises. Three different ways of thinking.

And honestly write code like Naruto. Take every result. Learn from the failures. Do not shut down when one thing breaks. Sit with the full picture and keep building.

Believe it.

More from this blog