Summary: In this tutorial, you will learn the significance of promise in JavaScript with the help of examples.

What is Promise in JavaScript?

In JavaScript, a promise is an object that represents the result of an asynchronous operation. A promise can be in one of three states: pending, fulfilled, or rejected.

A promise is created using the Promise constructor, which takes a function called the “executor function” as its argument.

The executor function is responsible for starting the asynchronous operation and is passed two functions as arguments: resolve and reject.

The resolve function is called when the asynchronous operation succeeds, and the reject function is called when the operation fails.

Here’s an example of how to create a promise:

const promise = new Promise((resolve, reject) => {
  // Asynchronous code goes here
  if (operationSucceeds) {
    resolve(value);
  } else {
    reject(error);
  }
});

Once a promise is created, you can use its then method to specify what should happen when the promise is fulfilled or rejected.

The then method takes two arguments: a callback function to be called when the promise is fulfilled, and another callback function to be called when the promise is rejected.

Here’s an example of how to use the then method:

promise.then(
  value => {
    // Code to be executed when the promise is fulfilled
  },
  error => {
    // Code to be executed when the promise is rejected
  }
);

Why Promise?

Promises are used to handle the result of asynchronous operations in JavaScript. They provide a way to write asyncronous code that is easier to read and debug than using callback functions.

One of the main advantages of promises is that they allow you to chain asyncronous operations together in a way that is easier to read and maintain than using nested callback functions.

For example, consider the following code that makes two HTTP requests using callback functions:

makeRequest('http://example.com/first', function(firstResponse) {
  makeRequest('http://example.com/second', function(secondResponse) {
    console.log(firstResponse, secondResponse);
  });
});

This code can be difficult to read, especially if there are many nested callback functions. Here’s the same code using promises:

makeRequest('http://example.com/first')
  .then(firstResponse => makeRequest('http://example.com/second'))
  .then(secondResponse => console.log(firstResponse, secondResponse));

This code is easier to read because the asyncronous operations are chained together in a way that reads like synchronous code.

Catch errors with Promise

Promises are also useful because they provide a way to handle errors that occur during asyncronous operations. With callback functions, it can be difficult to determine where an error occurred and to handle it appropriately.

With promises, you can use the catch method to handle any errors that occur during the asynchronous operation.

makeRequest('http://example.com/first')
  .then(firstResponse => makeRequest('http://example.com/second'))
  .then(secondResponse => console.log(firstResponse, secondResponse))
  .catch(error => console.log(error));

In this example, any errors that occur during the asyncronous operation will be caught by the catch method and logged to the console.

Adarsh Kumar

I am an engineer by education and writer by passion. I started this blog to share my little programming wisdom with other programmers out there. Hope it helps you.

Leave a Reply