Kumar Jitendra
5 min readNov 25, 2023

JavaScript — Different types of Promise Object Methods to handle the asynchronous call.

Introduction: -

In my previous blog, we have discussed around the definition of Promise Object in Java Script and how with the help of Promise Object in Java Script — it is easier to handle the different asynchronous calls.

Link for Previous blog: - JavaScript — Promises Introduction to handle the Asynchronous calls in Java Script. | by Kumar Jitendra | Nov, 2023 | Medium

We are going to discuss in the blog about the distinct types of Promise Methods available in Java Script: -

We will discuss about each of the Promise methods available in Java Script in details:-

a) all (iterable — array) method: — With the help of promise. all () — method we can catch multiple promise statements (multiple asynchronous functions) inside the array. Suppose if we have 3 different asynchronous functions — and we want to call the asynchronous functions in one go — with the help of promise. All () - we can pass all the functions inside the array and execute all the asynchronous functions in one go.

Example to demonstrate the promise.all() — method in Java Script method: -

Let's create three different asynchronous functions: -

Usage of promise.all(Array of functions) — We can pass an array of functions inside the Promise.all() method: -

Output: - We can check the Array Output of all the promise statements inside a single array — which will show all the collective function values from all the three functions: -

Disadvantage of Promise.all() method: - With the help of Promise.all() method — all the asynchronous functions need to be resolved — and if any of the asynchronous function throws error — it will always print the error statement — handled under the catch statement.

Example demonstrating the error statement — using the promise.all() method: -

With the promise.all() — though the first function is resolved — but the second function throws error — it will return only the rejected promise function.

Output: -

b) Promise.allSettled(Array of Iterables ) — With the help of Promise.allSettled() method, we can iterate over each of the functions whether its resolved or rejected — it doesn't matter, and it will collectively give all the combined results.

We can iterate using the for Each Array -each of the functions array of values and print each of the function value.

Below are the examples of the two Asynchronous functions which we have created:-

With the help of promise.allSettled () — we will iterate using for each Loop over each of the function -and all the values will be returned — it doesn't matter whether the function is resolve or rejected.

Output: — We can observe each of the function we are able to iterate whether the function is resolved or rejected — it doesn't matter.

Difference between the promise. All () and promise.allSettled() method: -

Promise.all() Method: - It will iterate over each of the promise functions- and if any of the function is rejected due to any reason — then it will return only the rejected response and will throw an error.

Promise.allSettled() Method: - It will iterate over each of the promise functions inside an array — and it doesn't matter whether the function is resolved or rejected.

c) Promise.any() Method: - This function waits for any of the function to resolved first. Suppose if we have 3 distinct functions and we wanted to know which function is resolved first — Promise.any() method will help in achieving this operation.

Examples demonstrating the Promise.any() method: -

Examples showing three different asynchronous functions and with the help of Promise.any () method -we wanted to know which of the function is resolved first: -

Output: - It will show the functions which one will get resolved first. In the below Output message — we can see check first function is printed in the Output console.

d) Promise.race() Method: - It is a method to check which of the asynchronous functions will get executed first. Suppose if we have 3 different asynchronous and we wanted to know which of the asynchronous function will get resolved first. Promise.race() — method will help in achieving this functionality to know which of the asynchronous functions get resolved first.

Examples demonstrating the Promise.race() method — suppose if we have two different asynchronous functions and we wanted to know which of the asynchronous function will get resolved first.

With the help of Promise.race() method — we will be able to find which of the asynchronous functions will get resolved first.

Output: - It will show the output which of the asynchronous function got resolved first and quicker.

Difference between the Promise.race and Promise.any function: -

Promise.any() Method: — It will show the output as which one of the asynchronous functions get resolved first.

Promise.race() Method: — It will show the output as which one of the asynchronous functions get resolved quicker and takes less time in resolving the function.

Conclusion: - In the following blog, we have discussed around the different types of methods available associated with the Promise class — to handle the asynchronous calls in the Java Script.

In the next blog — we will learn about the meaning of async and await concepts in Java Script.

Kumar Jitendra

An automation engineer.. enthusiastic about learning and exploring new things .