Skip to main content

Command Palette

Search for a command to run...

JavaScript Promise Methods

A deep, real-world explanation of Promises in JavaScript you’ll remember forever.

Updated
5 min read
JavaScript Promise Methods

Welcome to the world of JavaScript! If you have ever promised a friend you would call them back, you already understand the logic of a Promise.

In programming, things often take time like downloading a large image or fetching data from a server. Instead of making the whole program wait and freeze, JavaScript uses "Promises" to manage these tasks.


1. What is a Promise?

⤑ Imagine Jethalal wants to impress Babita Ji by bringing her a special "Chocolate Gunda" from Ahmedabad.

  • The Promise: Jethalal tells Babita Ji, "I will bring you the sweets by evening."

  • The Wait: Babita Ji doesn't know yet if she will get the sweets or if Jethalal will forget. She just has his "Promise."

⤑ In JavaScript, a Promise is an object representing the eventual completion (or failure) of an asynchronous operation and its resulting value.

2. The Three States of a Promise

⤑ Just like Jethalal’s commitment, every Promise goes through three stages:

  1. Pending: Jethalal is currently on his way. The result is unknown.

  2. Fulfilled (Resolved): Jethalal arrives with the sweets! Babita Ji is happy.

  3. Rejected: Jethalal gets stuck in a traffic jam or Bagha forgets to order them. Babita Ji is disappointed.

3. The Basic Methods: Handling One Promise

⤑ When you have a single promise, you use these three methods to handle the result.

then() – The Success Story

⤑ If Jethalal successfully brings the sweets, what should Babita Ji do? She handles the success using .then().

myPromise.then((message) => {
  console.log("Success: " + message);
});

⤑ This code says, "If the promise is fulfilled, take the result and do something with it."

catch() – The Backup Plan

⤑ If Jethalal fails (the promise is rejected), Babita Ji needs to handle the error. She uses .catch().

myPromise.catch((error) => {
  console.log("Error: " + error);
});

⤑ This code says, "If something goes wrong, tell me what the error is so I can fix it."

finally() – The Final Word

⤑ Whether Jethalal brings the sweets or fails, Bapuji will definitely give him a lecture. This happens no matter what.

myPromise.finally(() => {
  console.log("Process finished. Bapuji starts his lecture.");
});

⤑ This code runs at the very end, regardless of whether the promise succeeded or failed.


4. Handling Multiple Promises (Static Methods)

⤑ Sometimes, we have many promises happening at once. This is where things get interesting in Gokuldham!

Promise.all() – The Team Player

Scenario: Tapu Sena (Tapu, Sonu, Goli) decides to clean the clubhouse.

  • If all of them finish their work, the party starts.

  • If even one person fails, the whole plan is cancelled.

Promise.all([promiseTapu, promiseSonu, promiseGoli])
  .then((results) => console.log("Clubhouse is clean!"))
  .catch((err) => console.log("Someone failed, party cancelled!"));

⤑ It waits for all to succeed. If one fails, the whole thing fails immediately.

Promise.allSettled() – The Report Card

Scenario: Bhide gives different tasks to everyone for Ganpati Festival.

  • Bhide wants a report of who finished and who failed.

  • He doesn't stop the festival if one person fails; he just wants to know the status of everyone.

Promise.allSettled([task1, task2, task3])
  .then((results) => console.log("Here is the status of every task."));

Behavior: It waits for all promises to finish (either success or failure) and returns an array of results.

Promise.race() – The Fast and Curious

Scenario: Jethalal, Bhide, and Iyer are all racing to reach the Soda Shop first to talk to Abdul.

  • The person who reaches first wins.

  • It doesn't matter if they succeeded or failed; the first one to "cross the finish line" (settle) wins.

Promise.race([jethaRun, bhideRun, iyerRun])
  .then((winner) => console.log("The first person reached!"));

Behavior: It returns the result of the first promise that finishes, whether it's a success or an error.

Promise.any() – The First Success

Scenario: Popatlal is looking for a marriage proposal. He sends his profile to three different matchmakers.

  • He only cares about the first one who says "Yes!" (success).

  • If the first one says "No," he waits for the second one.

  • He only gets sad if everyone says "No."

Promise.any([matchMaker1, matchMaker2, matchMaker3])
  .then((firstYes) => console.log("Popatlal is getting married!"))
  .catch((err) => console.log("All rejected. Cancel the umbrella."));

Behavior: It waits for the first successful promise. It only fails if every single promise fails.


❊ Table :

Method

Behavior

.then()

Runs when a promise succeeds.

.catch()

Runs when a promise fails.

.finally()

Runs always, regardless of success or failure.

Promise.all()

Succeeds if all succeed; fails if any fail.

Promise.allSettled()

Waits for all to finish, no matter the outcome.

Promise.race()

Returns the first promise to finish (win or lose).

Promise.any()

Returns the first success; fails only if all fail.


❊ Conclusion

JavaScript Promises are just like the commitments made in Gokuldham Society. They help us manage time, handle failures gracefully, and coordinate between different tasks. Just remember: keep your promises like Jethalal tries to (mostly) and handle your errors like Bhide handles his "Zamana!"


Want More…?

I write articles on blog.prakashtsx.com and also post development-related content on the following platforms: