Creating a promise, Chaining and Error Handling:

Creating a promise, Chaining and Error Handling:

Table of contents

No heading

No headings in the article.

Here, we are creating createOrder function by ourselves and this function will return a promise. To create a promise we will use a new keyword and Promise() constructor. Now how we will call this Promise() constructor? This promise() constructor will take a function(executor function) and this function has two parameters which are resolve and reject.

Resolve and Reject:

The executor function takes two arguments, resolve and reject. These are the callbacks provided by the JavaScript language. Your logic goes inside the executor function that runs automatically when a new Promise is created.

continue...

now, if we invoke Promise() constructor using a new keyword and pass executor function with resolve and reject parameter, now we can just return the promise.

What do we do inside a promise?

Inside a promise, we will write a logic for handling whatever we need to do inside inside createOrder function.

Let's take a dummy example:

If we are trying to validate a cart, we have a function validateCart() if this function fails we will reject this Promise. Understand this, here we are creating a promise and we are sending it. Now, what will happen we can resolve it or we can reject it and there is a logic behind it. Suppose the cart is not validated(!validateCart()), now we will throw an error and reject the promise. How can we reject the Promise? Here, we will call reject() function with some error, if we reject a promise in the producer part so our promise in the customer part is also rejected.

Now, if everything is fine, and the function is validated, we will write the logic for createOrder if the logic of createOrder is successful then we will get some order Id and if the orderId is valid we will just resolve our promise. A promise can either be fulfilled or rejected or resolved. Now we can resolve the error by passing the resolve function with orderId parameter.

Error Handling:

The way you catch an error in a promise is you chain a .catch() to it.

Catch will pass you the error as a parameter which we will use to log the error.

Promise Chaining:

Now we want to proceed to payment:

This is how we chain things up. This is the promise chaining. Once we get the orderId from createOrder() promise, then we will pass the data to the next level of the chain, and then we will proceed to payment proceedToPayment() . And proceedToPayment() will again return a promise. So proceedToPayment() is a promise now.

Now, this proceedToPayment() will return a promise. Last time what we did we just created a pr variable with new Promise() and then just returned pr. Here, in proceedToPayment() instead of that directly write return new Promise() and what does this promise constructor take - a function and this function has two arguments resolve and reject

Advance Promise Chaining:

In the below code, whatever the response of the ProceedToPayment will be passed to the next callback function of this chain and then we will get paymentInfo and this is how we chain things up but there is a very common error in this program and you won't be able to figure out what the error is.

Whenever we are chaining things up in this promise chain we need to keep returning things from one chain down to the other that's how we do it. so whatever we need to pass it down we need to return it from the top of the chain ok. Or what we can return from here we can return any data or we can return a promise that can be resolved. when we return promise proceedToPayment() this .then will get attached to proceedToPayment() promise.

Advance Error Handling:

This is a good way of writing code:

The above code is similar to the below code... But this is not the right way of writing code

But this code is ugly. we again fall into promise hell. If we keep writing like this, it will be a promise hell. But the promise API is designed in such a way that we don't fall into this callback hell or that Doom "Pyramid of Doom" like structure. So what we do is we generally return the promise from this and we handle it in the next level of the chain.

And one of the most important things is if there is a big promise chain and there is any error on the part of this chain, the catch will handle any error that is down in the chain.

How it works:

If the validateCart() returns true, after 5 seconds createOrder() promise will be resolved and it will print the orderId and this orderId will pass down the chain and this orderId was sent to proceedToPayment() . Now proceedToPayment() just send the data to the attached function i.e. paymentInfo() and then paymentInfo will be printed.

What if validateCart() returns false, it will be logged in the console "cart is not valid".

What if we want to go to proceedToPayment() even if the validateCart() returns false?

What we will do is we will put .catch() function just after orderId .Now what will happen is this catch() has the responsibility to only check the top of it. So it will only be concerned which are occurring on the top of the chain. And if logged it proceedToPayment() will still happen.