# 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()`.

```javascript
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()`.

```javascript
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.

```javascript
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.
    

```javascript
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**.
    

```javascript
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.
    

```javascript
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."
    

```javascript
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 :

<table style="min-width: 390px;"><colgroup><col style="width: 365px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1" colwidth="365"><p><strong><em>Method</em></strong></p></td><td colspan="1" rowspan="1"><p><strong><em>Behavior</em></strong></p></td></tr><tr><td colspan="1" rowspan="1" colwidth="365"><p><strong>.then()</strong></p></td><td colspan="1" rowspan="1"><p>Runs when a promise succeeds.</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="365"><p><strong>.catch()</strong></p></td><td colspan="1" rowspan="1"><p>Runs when a promise fails.</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="365"><p><strong>.finally()</strong></p></td><td colspan="1" rowspan="1"><p>Runs always, regardless of success or failure.</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="365"><p><strong>Promise.all()</strong></p></td><td colspan="1" rowspan="1"><p>Succeeds if <strong>all</strong> succeed; fails if <strong>any</strong> fail.</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="365"><p><strong>Promise.allSettled()</strong></p></td><td colspan="1" rowspan="1"><p>Waits for <strong>all</strong> to finish, no matter the outcome.</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="365"><p><strong>Promise.race()</strong></p></td><td colspan="1" rowspan="1"><p>Returns the <strong>first</strong> promise to finish (win or lose).</p></td></tr><tr><td colspan="1" rowspan="1" colwidth="365"><p><strong>Promise.any()</strong></p></td><td colspan="1" rowspan="1"><p>Returns the <strong>first success</strong>; fails only if <strong>all</strong> fail.</p></td></tr></tbody></table>

* * *

## ❊ 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**](http://blog.prakashtsx.com) and also post development-related content on the following platforms:

*   [**Twitter/X**](https://x.com/prakashtsx)
    
*   [**LinkedIn**](https://www.linkedin.com/in/prakashtsx)
