Explain Promise.allSettled() with async-await in JavaScript
Last Updated :
21 Nov, 2023
In this article, we will try to understand how we may implement the Promise.allSettled() method with async-await in JavaScript with the help of certain coding examples as well as theoretical explanations. Let us first quickly understand about Promise.allSettled() method. This method is one of the most common methods available under the Promise object which executes once all the promises are either successfully resolved or rejected. The output of this method will be an array of multiple objects (depending upon as many promises created by the user), containing the value and status of each promise in each object itself. It is better to use Promise.all() if tasks are dependent on each other or if we want to reject a particular promise at any point.
Syntax:
The following syntax will be preferred to be used while implementing the Promise.allSettled() method:
Promise.allSettled([first_promise, second_promise, ...]).then(
// do something...
)
Now after analyzing the above syntax, we will see the following example that will help us to understand the above syntax in a much better and more efficient manner:
Example 1: In this example, we will be creating three promises one after the another. Each promise will contain a different timer function having different timeouts. Afterward, we will use Promise.allSettled() method which will take all three promises as input in the form of an array (or an iterable object) and executes the result as per its role.
JavaScript
let first_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("GeeksforGeeks...!!");
}, 1000);
});
let second_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("JavaScript......!!");
}, 2000);
});
let third_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("TypeScript...!!");
}, 3000);
});
let promise_array = [first_promise, second_promise, third_promise];
Promise.allSettled(promise_array).then((result) => console.log(result));
Output:
[
{ status: 'fulfilled', value: 'GeeksforGeeks...!!' },
{ status: 'fulfilled', value: 'JavaScript......!!' },
{ status: 'fulfilled', value: 'TypeScript...!!' }
]
Now we have seen how to implement Promise.allSettled() method, let us see how to implement this method with the help of async-await keywords.
Example 2: In this example, we will be creating three promises one after the another (like we did in the previous example). Each promise will contain a different timer function having different timeouts. Then we will create a function with the prefixed async keyword in it and inside it, we will catch or store our result using await keyword. Afterward, we will use Promise.allSettled() method which will take all three promises as input in the form of an array (or an iterable object) and executes the result as per its role.
JavaScript
let first_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("GeeksforGeeks...!!");
}, 1000);
});
let second_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("JavaScript......!!");
}, 2000);
});
let third_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("TypeScript...!!");
}, 3000);
});
async function displayResult() {
let promise_array = [first_promise, second_promise, third_promise];
let result = await Promise.allSettled(promise_array);
console.log(result);
}
displayResult();
Output:
[
{ status: 'fulfilled', value: 'GeeksforGeeks...!!' },
{ status: 'fulfilled', value: 'JavaScript......!!' },
{ status: 'fulfilled', value: 'TypeScript...!!' }
]
Similar Reads
Explain Promise.all with async-await in JavaScript In JavaScript, Promise.all with async-await is used to handle multiple asynchronous operations concurrently. By combining Promise.all with await, you can wait for all promises to resolve or any to reject, ensuring that all asynchronous tasks are complete before proceeding with the next code executio
4 min read
Explain Promise.any() with async-await in JavaScript In this article, we will try to understand how to implement the Promise.any() method with async-await in JavaScript using some theoretical explanations followed by some coding examples as well. Let us firstly quickly understand the working of Promise.any() method with some theoretical examples (incl
4 min read
Explain Promise.race() with async-await in JavaScript In this article, we will try to understand how we may implement Promise.race() method with async-await in JavaScript with the help of certain coding examples as well as theoretical explanations. Let us first quickly understand how we may implement Promise.race() method. This method is one of the mos
3 min read
Explain Async Await with Promises in Node.js Async/Await in Node.js is a powerful way to handle asynchronous operations. It allows you to write asynchronous code in a synchronous manner, making it easier to read, write, and debug. This feature is built on top of Promises, which represent a value that may be available now, or in the future, or
4 min read
How to delay a loop in JavaScript using async/await with Promise ? In JavaScript, you can delay a loop by using async/await with Promise. By wrapping a setTimeout() inside a Promise and using await, you can pause execution at each iteration, creating delays between loop iterations for asynchronous tasks without blocking the main thread.What is async and await?async
2 min read
How to use async/await with forEach loop in JavaScript ? Asynchronous is popular nowadays because it gives functionality of allowing multiple tasks to be executed at the same time (simultaneously) which helps to increase the productivity and efficiency of code. Async/await is used to write asynchronous code. In JavaScript, we use the looping technique to
2 min read
Async and Await in JavaScript Async and Await in JavaScript are used to simplify handling asynchronous operations using promises. By enabling asynchronous code to appear synchronous, they enhance code readability and make it easier to manage complex asynchronous flows.JavaScriptasync function fetchData() { const response = await
3 min read
JavaScript - How to Handle Errors in Promise.all? To handle errors in Promise.all(), you need to understand that it fails immediately if any of the promises reject, discarding all resolved values. This behavior can lead to incomplete operations and potential application crashes. The best way to manage errors in Promise.all() is by using .catch() in
3 min read
Explain Scope and Scope Chain in JavaScript In this article, we will try to understand what is the scope of a variable as well as its function (or method). We will see what is a Scope Chain with the help of certain coding examples. ScopeScope in JavaScript determines the accessibility of variables and functions at various parts of one's code
5 min read
JavaScript Promise allSettled() Method Promise.allSettled() method in JavaScript is used to handle multiple promises concurrently and return a single promise. This promise is fulfilled with an array of promise state descriptors, each describing the outcome of the corresponding promise in the input array. Unlike Promise.all(), Promise.all
2 min read