What is Promise in Javascript with real world example
Tags: javascript, webdev, frontend
Published: 2024-04-17 03:09:51

<h3 id="introduction-to-promise-in-javascript">Introduction to Promise in JavaScript</h3>
Asynchronous programming is a fundamental aspect of JavaScript, as it allows for non-blocking operations and enhances the overall performance of web applications. However, handling asynchronous operations can become complex and lead to “callback hell” or nested callbacks. To address this issue and provide a more structured approach, Promises were introduced in JavaScript.

Promises are a built-in feature introduced in ECMAScript 2015 (ES6) that represent the eventual completion (or failure) of an asynchronous operation. They provide a way to write cleaner, more readable, and maintainable asynchronous code by simplifying the handling of asynchronous operations and their results.

At its core, a Promise is an object that can be in one of three states: pending, fulfilled, or rejected.

- **Pending**: The initial state when a Promise is created. It represents an ongoing, unresolved asynchronous operation.
- **Fulfilled**: The state when the asynchronous operation is successfully completed. The Promise is fulfilled, meaning it completed successfully, and it holds a value.
- **Rejected**: The state when an error occurs during the asynchronous operation. The Promise is rejected, indicating that an error occurred, and it holds a reason or an error object.

![Promise with three states](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gcnp07gzqf4xmdov1edd.png)

The key concept behind Promises is the ability to attach callbacks to handle the fulfillment or rejection of the asynchronous operation. These callbacks are registered using the `.then()` method on a Promise instance. The `.then() `method takes two optional arguments: a callback function to handle the fulfilled state and a callback function to handle the rejected state.

Here’s an example that demonstrates the basic usage of a Promise:

```php
const myPromise = new Promise((resolve, reject) => {
  // Asynchronous operation
  setTimeout(() => {
    const data = "Promise resolved successfully!";
    resolve(data); // Fulfill the Promise
    // Or reject the Promise
    // reject(new Error("An error occurred")); 
  }, 2000);
});
 
myPromise
  .then(result => {
    console.log("Promise fulfilled:", result);
  })
  .catch(error => {
    console.error("Promise rejected:", error);
  });
```

<p>In this example, we create a new Promise using the <code>Promise</code> constructor. Inside the Promise’s executor function, we perform an asynchronous operation, which in this case is simulated using <code>setTimeout()</code>. After the operation completes successfully, we call <code>resolve()</code> with the data we want to pass as the fulfillment value.</p>

<p>We chain a <code>.then()</code> method to the Promise instance to register a fulfillment callback that will be called when the Promise is fulfilled. Inside the callback, we log the fulfillment value to the console.</p>

<p>If the Promise is rejected, either due to an error or by explicitly calling <code>reject()</code>, the rejection callback specified using <code>.catch()</code> will be invoked, and we can handle the error accordingly.</p>

<p>Promises provide a more structured and readable way to handle asynchronous operations and their outcomes in JavaScript. They allow you to write asynchronous code that resembles synchronous code, making it easier to reason about and maintain. Promises also offer additional features like chaining, error propagation, and advanced composition using <code>Promise.all()</code> and <code>Promise.race()</code>, which further enhance their utility in modern JavaScript development.</p>

<h3 id="how-promise-works-under-the-hood">How Promise works 
under the hood</h3>

<p>Promises are an integral part of JavaScript’s asynchronous programming model and provide a way to handle asynchronous operations in a more structured and manageable manner. To understand how Promises work under the hood, let’s delve into their inner workings.</p>

<h4 id="promise-creation">Promise Creation</h4>

<p>When you create a Promise using the <code>new Promise()</code> syntax, you provide an executor function as its argument. This executor function is called immediately when the Promise is created. It receives two callback functions as parameters: <code>resolve</code> and <code>reject</code>. Inside the executor function, you perform your asynchronous operation and call either <code>resolve(value)</code> to fulfill the Promise or <code>reject(reason)</code> to reject it.</p>

```php
const myPromise = new Promise((resolve, reject) => {
  // Asynchronous operation
  setTimeout(() => {
    const data = "Some result";
    resolve(data); // Fulfill the Promise
    // Or
    // reject(new Error("An error occurred")); // Reject the Promise
  }, 2000);
});
```

<h4 id="promise-states">Promise States</h4>

<p>Promises have three possible states:</p>
<ul>
<li><strong>Pending</strong>: The initial state when the Promise is created. It is neither fulfilled nor rejected.</li>
<li><strong>Fulfilled</strong>: The state when the asynchronous operation is successfully completed. The Promise is fulfilled, and it holds a value.</li>
<li><strong>Rejected</strong>: The state when an error occurs during the asynchronous operation. The Promise is rejected, and it holds a reason or error object.</li>
</ul>
<p>The Promise starts in the pending state and transitions to either the fulfilled or rejected state based on the outcome of the asynchronous operation.</p>

<h4 id="chaining-promises">Chaining Promises</h4>

<p>One of the key features of Promises is the ability to chain them together, enabling sequential execution of asynchronous operations and avoiding callback hell. To chain Promises, you use the <code>.then()</code> method, which takes a callback function as its argument. This callback function receives the resolved value from the previous Promise and can return another Promise or a value. The <code>.then()</code> method returns a new Promise, allowing you to chain multiple <code>.then()</code> calls together.</p>

```php
myPromise
  .then(result => {
    console.log("First operation result:", result);
    return anotherAsyncOperation();
  })
  .then(anotherResult => {
    console.log("Second operation result:", anotherResult);
    // Perform additional operations or return a value
  })
  .catch(error => {
    console.error("An error occurred:", error);
  });
```

<p>In this example, the first <code>.then()</code> callback receives the resolved value from <code>myPromise</code> and performs an additional asynchronous operation. It returns another Promise, which becomes the input for the second <code>.then()</code> callback.</p>

<h4 id="asynchronous-execution">Asynchronous Execution</h4>

<p>Promises utilize an event-driven mechanism and leverage the JavaScript event loop to execute asynchronous operations. When a Promise is created, the executor function is called immediately, and the asynchronous operation starts. The Promise registers callbacks internally and allows other synchronous operations to continue while it awaits the completion of the asynchronous task.</p>

<h4 id="resolution-and-rejection">Resolution and Rejection</h4>

<p>Once the asynchronous operation completes, the Promise transitions to the fulfilled state if it was successful and the <code>resolve</code> function was called. The resolved value is then passed to the corresponding <code>.then()</code> callback for further processing.</p>
<p>If an error occurs during the asynchronous operation and the <code>reject</code> function is called, the Promise transitions to the rejected state. The reason or error object passed to <code>reject</code> is propagated to the nearest <code>.catch()</code> block or the subsequent <code>.catch()</code> callbacks in the Promise chain.</p>

<h4 id="error-handling">Error Handling</h4>

<p>Promises provide built-in error handling through the <code>.catch()</code> method. By chaining a <code>.catch()</code> call at the end of the Promise chain, you can capture and handle any errors that occurred in any of the preceding Promises. If any Promise in the chain is rejected, the control flow skips to the nearest <code>.catch()</code> block, allowing you to handle the error gracefully.</p>
<p>Promises internally manage the propagation of errors through the chain, ensuring that any unhandled rejections are caught and reported.</p>

<h4 id="promise-all-and-promise-race">Promise.all() and Promise.race()</h4>

<p>Promises offer utility methods like <code>Promise.all()</code> and <code>Promise.race()</code> to handle multiple Promises simultaneously.</p>
<ul>
<li><code>Promise.all()</code> takes an array of Promises as its argument and returns a new Promise that resolves to an array containing the resolved values of all the Promises. It waits for all Promises in the array to settle, and if any Promise rejects, the entire <code>Promise.all()</code> operation is rejected.</li>
<li><code>Promise.race()</code> takes an array of Promises and returns a new Promise that settles as soon as any of the Promises in the array settles (fulfills or rejects). It resolves or rejects with the value or reason of the first settled Promise.</li>
</ul>
<p>These methods are useful for handling scenarios where you need to wait for multiple asynchronous operations to complete concurrently or track the completion of the fastest operation.</p>
<p>Overall, Promises provide a structured and powerful mechanism to handle asynchronous operations in JavaScript. They allow for cleaner and more readable code by enabling the chaining of asynchronous operations and offering built-in error handling capabilities.</p>

<h3 id="example-using-promise-in-real-world-scenario">Example using Promise in real world scenario</h3>

<p>Consider a weather application that fetches weather data from an API. Let’s create a function <code>getWeatherData()</code> that uses Promises to handle the asynchronous fetch operation:</p>

```php
function getWeatherData() {
  return new Promise((resolve, reject) => {
    const apiKey = 'your-api-key';
    const apiUrl = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&amp;q=London`;
 
    fetch(apiUrl)
      .then(response => {
        if (response.ok) {
          return response.json();
        } else {
          throw new Error('Unable to fetch weather data.');
        }
      })
      .then(data => {
        resolve(data);
      })
      .catch(error => {
        reject(error);
      });
  });
}
```

<p>In this example, we use the <code>fetch()</code> API to make an HTTP request to retrieve the current weather data for London. The <code>fetch()</code> function returns a Promise that resolves to the <code>Response</code> object.</p>
<p>Inside the Promise’s executor function, we handle the asynchronous operation using the <code>.then()</code> method. If the response is successful (<code>response.ok</code> is <code>true</code>), we parse the JSON data and resolve the Promise with the resulting data. If there is an error, we throw a new Error object and reject the Promise.</p>
<p>Now, let’s use the <code>getWeatherData()</code> function and chain Promises to handle the weather data retrieval and display:</p>

```php
getWeatherData()
  .then(data => {
    const weather = data.current;
    console.log(`Temperature in ${data.location.name}: ${weather.temp_c}°C`);
    console.log(`Condition: ${weather.condition.text}`);
  })
  .catch(error => {
    console.error('Error fetching weather data:', error);
  });
```

<p>In this example, we call the <code>getWeatherData()</code> function and chain a <code>.then()</code> callback to handle the resolved data. We extract the necessary information from the <code>current</code> property of the data object and log the temperature and weather condition to the console.</p>
<p>If there is an error during the retrieval or parsing of the weather data, the <code>.catch()</code> callback will be triggered, and we log the error message to the console.</p>
<p>By using Promises, we can handle the asynchronous nature of the weather data retrieval, ensure proper error handling, and create a more readable and manageable code flow.</p>
<p>Note: In a real-world application, you would typically handle more complex scenarios, such as handling multiple API requests concurrently using <code>Promise.all()</code>, transforming and combining data from different Promises, or incorporating async/await syntax for even cleaner asynchronous code.</p>

<h3 id="how-to-handle-multiple-promises">How to Handle Multiple Promises</h3>

![Multiple Promises](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6mq4cecebpts5hxssmso.png)

<p>Handling multiple Promises in JavaScript can be done using Promise.all() or Promise.race(). These methods allow you to work with multiple Promises concurrently and handle their results in different ways. Here’s how you can use them:</p>

<h4 id="promise-all">Promise.all()</h4>

<p>The Promise.all() method takes an array of Promises as its argument and returns a new Promise that resolves when all the Promises in the array have resolved successfully. If any of the Promises in the array is rejected, the entire Promise.all() operation is rejected. The resolved values of the individual Promises are collected into an array in the same order as the original array.<br>
Here’s an example:</p>

```php
const promise1 = new Promise(resolve => setTimeout(() => resolve('Promise 1'), 2000));
const promise2 = new Promise(resolve => setTimeout(() => resolve('Promise 2'), 3000));
const promise3 = new Promise(resolve => setTimeout(() => resolve('Promise 3'), 1000));
 
Promise.all([promise1, promise2, promise3])
  .then(results => {
    console.log('All Promises resolved:', results);
  })
  .catch(error => {
    console.error('At least one Promise rejected:', error);
  });
```

<p>In this example, we create three Promises that resolve after different delay times using setTimeout(). We pass an array containing these Promises to Promise.all(). The .then() callback is called when all the Promises have resolved, and the resolved values are logged to the console. If any of the Promises is rejected, the .catch() callback is triggered.</p>

<h4 id="promise-race">Promise.race()</h4>

<p>The Promise.race() method takes an array of Promises and returns a new Promise that settles (resolves or rejects) as soon as any of the Promises in the array settles. It resolves with the value of the first Promise that resolves or rejects with the reason of the first Promise that rejects.<br>
Here’s an example:</p>

```php
const promise2 = new Promise(resolve => setTimeout(() => resolve('Promise 2'), 3000));
const promise3 = new Promise((resolve, reject) => setTimeout(() => reject(new Error('Promise 3 Error')), 1000));
 
Promise.race([promise1, promise2, promise3])
  .then(result => {
    console.log('First Promise resolved:', result);
  })
  .catch(error => {
    console.error('First Promise rejected:', error);
  });
```

<p>In this example, we have three Promises again with different delay times. However, this time, we use Promise.race() instead of Promise.all(). The .then() callback is called with the result of the first Promise that resolves or the .catch() callback is triggered with the error of the first Promise that rejects.</p>
<p>By using Promise.all() and Promise.race(), you can effectively handle multiple Promises and control the flow based on the collective results or the first settled Promise. These methods are especially useful when you need to wait for multiple asynchronous operations to complete concurrently or track the completion of the fastest operation.</p>