JavaScript Promise Chaining!

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

In this article, we are going to learn about how to use Promise Chaining (Basics) in JavaScript.

We will cover the following points:-

  • Promise.
  • Declaring a Promise.
  • Executing a Promise.
  • Promise Chaining.

So let's get started!

Promise Definition

A Promise is basically an object which represents the completion (or failure) of an asynchronous operation along with its result.

In a descriptive way, we can say that A Promise is an object representing the eventual completion or failure of an asynchronous operation. Since most people are consumers of already-created promises.

let's understand the states of promise. These states are responsible for the results.

  • Pending: It represents either an initial state or fulfilled state or rejected state.
  • Fulfilled: It represents that the asynchronous operation is successfully completed.
  • Rejected: This state represents that the asynchronous operation is rejected

1.PNG

Declaring a Promise

let promise =  new Promise((resolve , reject)=>{
                resolve('Welcome To Hashnode !');
});

You can see in the above syntax, there is a callback function that is passed inside a promise object which takes two methods as an argument.

First, one is resolve() which is responsible for the successful completion of whatever text or anything executable is passed inside it.

And the other one is reject() which is responsible for the unsuccessful completion of an operation, and we can pass on text inside it, which gets displayed along with our error.

Executing a Promise!

<script>let promise = new Promise((resolve, reject) => {
  resolve("Welcome To Hashnode!");
});

promise.then((result) => {
  console.log(result);
});
</script>

In the callback function result, the variable is declared which is responsible for printing out the result which is coming from the resolve() method.

Output: Welcome To Hashnode!

Now let's Come to our Final point.

Promise Chaining

It is a simple concept by which we may initialize another promise inside our .then() method and accordingly we may execute our results.

A common need is to execute two or more asynchronous operations back to back, where each subsequent operation starts when the previous operation succeeds, with the result from the previous step. We accomplish this by creating a promise chain.

Syntax :


<script>
let promise = new Promise((resolve, reject) => {
  resolve("Hello Coders");
});
promise
  .then(
    new Promise((resolve, reject) => {
      resolve("Welcome To Hashnode Articles");
    }).then((result1) => {
      console.log(result1);
    })
  )
  .then((result2) => {
    console.log(result2);
  });
</script>

Output : Welcome To Hashnode Articles
Hello Coders

So this is it for this guide.

I hope you liked it!. Let's catch up in the next articles and Stay tuned.

If you liked it, Please Support Me

Interested in reading more such articles from Nishant Gour?

Support the author by donating an amount of your choice.

Recent sponsors
keerti shukla's photo

you are doing an amazing job!

Shree krishna lamichhane's photo

Explained in simplicity. Good Job. I loved it.😍

Nishant Gour's photo

Thank you Shree krishna lamichhane. I am glad you liked it.