Javascript Promises and its method

21 Jun, 2021 / Blog Posts By: Thomas George

Understanding JavaScript Promises and its method

 

Understanding JavaScript Promises and its method, Javascript promises is a feature that most developers initially consider as complex logic. It is actually one of the easiest and powerful features in javascript. This feature can be used in any javascript based framework.

In this post, I will be explaining in detail javascript promises and how to use it.

So let’s get started.

So what is a promise in the real world?

A promise is a statement which you make to a person in which you say that you will definitely do something or give them something. This is exactly what a promise in javascript does.

 

When you write a promise in javascript it can be in three states pending, fulfilled, or rejected. Let’s take a simple example to understand this in a broader way.

Suppose you want to write a function to add two numbers and if the sum is 5 then the function returns “success” and if it’s any other value then the function returns “failure”.If you are writing a function with a return statement then you can achieve this in the following way.

 

function returns “success” and function returns “failure”

 

 

Let’s achieve the same with javascript promise. The syntax of javascript promise is as follows.

two variables called resolve and reject

 

This is how you define a promise in javascript. Promise is an object which takes in a function and this function takes in two variables called resolve and reject. 

 

What is the resolve() method in the javascript promise? 

 

I have seen many complex explanations of this. But I will explain this in the simplest form possible, resolve is a method that we use to return a success response when the execution inside a promise is complete. A successful response can be anything ie it can be an object, a string, an array, etc. Below are some examples of how to use the resolve method.

 

 

 

 

What is the reject() method in javascript promise? 

 

reject is a method that we use to return a failure response when the execution inside a promise is complete. A failure response can be anything i:e it can be an object,a string,an array,etc.Below are some examples of how to use reject method

 

 

 

Now that we have gone through the syntax of promise, let’s use it to do our first example with javascript promise. Below is the code for this.

 

 

 

 

What I have shown here is a simple example. In javascript, we use promises to run more complex logic which takes some time to complete,such as upload an image and return a response when the upload is complete or call a third party API and wait for its response, etc. So basically promises are used to execute an asynchronous operation.

 

Now let’s see how to access a result from a promise. For this, we have two methods 

then() and catch()

 

1).then(): The resolved response from a promise can be received using this method.

2).catch(): The rejected response from a promise can be received using this method.

 

Both of the above methods take in a function that takes in a variable and this variable name can be anything that you want.

 

Let’s use the example that we saw earlier. Below is the code for achieving this.

 

 

In this example, we can see that the function addTwoNumbers is called and a promise is executed and a response is returned. This function will execute asynchronously.

 

There are several ways in which we can use javascript promises and its methods. In the upcoming post’s I will be explaining how to use other promise methods as well as how to use await with promises.

Hope you like the content. 

Thank you!