How does Promise.all() method differs from Promise.allSettled() method in JavaScript ?
Last Updated :
01 May, 2023
In this article, we will first understand in brief the Promise.all() as well as Promise.allSettled() methods and then we will try to visualize how they differ from each other through some theoretical as well as some coding examples.
Both Promise.all() and Promise.allSettled() methods are the methods of a Promise object (which is further a JavaScript object used to handle all the asynchronous operations) that are used to handle multiple promises results simultaneously.
Promise.all() method: method returns a single Promise after receiving one or more promises as input. when all of the Promises in the input are satisfied, the returning promise is fulfilled. when any of the inputs, or promises are refused, it rejects a promise with this first rejection reason.
Syntax: The following syntax could be used for declaring this method.
Promise.all([promise_1 , promise_2, ...]).then(
// do something...
)
Promise.allSettled() method: method returns a single Promise from one or more promises that are passed in as input when all of the input’s promises have been settled (including empty iterable), this promise is fulfilled and an array of objects containing a description of each promise’s result is returned.
Syntax: The following syntax could be used for declaring this method.
Promise.allSettled([promise_1 , promise_2, ...]).then(
// do something...
)
Now after analyzing their syntaxes, it’s high time to look into the fact that how they actually differ from each other.
How do they differ from each other?
Following are some of the most important points which will help us to understand their difference (and that too with the help of some coding examples):-
In terms of rejecting promises:
- Promise.all() method rejects itself if any of the passed-in promise input inside an array is rejected. That is, this method will only run if and only if all the promises are fulfilled or resolved successfully, otherwise, in the output, it would produce an error message.
- Promise.allSettled() method will not reject itself if any of the passed-in promise input inside an array is rejected. That is, this method will irrespective of any promise be in the rejected state too.
In terms of their outputs:
- Promise.all() method returns an array as an output containing promise data inside several indexes.
- Promise.allSettled() method returns an array of objects and each of these objects further contains two properties further status and value.
Let us know some of the below-mentioned coding examples in order to better understand the above-illustrated facts:-
Example 1: In this example, we will create two promises which are resolved successfully, and then we will pass them inside both of these methods and will visualize their result.
Code block
Output:
[ ‘Resolved First after 1 second’, ‘Resolved First after 2 seconds’ ]
[
{ status: ‘fulfilled’, value: ‘Resolved First after 1 second’ },
{ status: ‘fulfilled’, value: ‘Resolved First after 2 seconds’ }
]
Example 2: In this example, we will pass one rejected promise in both of these methods and further will visualize their outputs.
Javascript
let rejected_promise = new Promise((resolve,
reject) => reject( "Rejected Promise...." ));
let first_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve( "Resolved First after 1 second" );
}, 1000);
});
let second_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve( "Resolved First after 2 seconds" );
}, 2000);
});
try {
let result_1 = Promise.all([rejected_promise,
first_promise, second_promise]);
result_1.then((data) => console.log(data));
let result_2 = Promise.allSettled([
rejected_promise,
first_promise,
second_promise,
]);
result_2.then((data) => console.log(data));
} catch (error) {
console.log(error);
};
|
Output:
(node:10372) UnhandledPromiseRejectionWarning: Rejected Promise….
(Use `node –trace-warnings …` to show where the warning was created)
(node:10372) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
This error originated either by throwing inside of an async function without a catch block,
or by rejecting a promise which was not handled with .catch(). To terminate the node process
on unhandled promise rejection, use the CLI flag `–unhandled-rejections=strict`
see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:10372) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated.
In the future, promise rejections that are not handled will terminate the Node.js process
with a non-zero exit code.
[
{ status: ‘rejected’, reason: ‘Rejected Promise….’ },
{ status: ‘fulfilled’, value: ‘Resolved First after 1 second’ },
{ status: ‘fulfilled’, value: ‘Resolved First after 2 seconds’ }
]
Similar Reads
How does Promise.any() method differs from Promise.race() method in JavaScript ?
In this article, we will first try to understand how we may declare or use Promise.any() and Promise.race() methods followed by some facts which will eventually help us to understand how they differ from each other (through theoretical as well as coding examples). Let us first quickly understand in
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
Implement polyfill for Promise.all() method in JavaScript
The task is to write a polyfill for Promise.all methods in javascript. What is a Polyfill? A polyfill is a piece of computer code written to implement a feature in a browser that does not yet support it. It could be because the older version of the browser you are using, or the new version of the br
4 min read
How to use every() or some() methods in JavaScript ?
In JavaScript, the every() and some() methods are array methods used to check the elements of an array based on a given condition. every(): Checks if all elements in an array satisfy a condition.some(): Checks if at least one element in an array satisfies a condition.Table of Content every() Methods
2 min read
What is the difference between every() and some() methods in JavaScript ?
In this article, we will see the difference between every() and some() methods in JavaScript. Array.every() methodThe Array.every() method in JavaScript is used to check whether all the elements of the array satisfy the given condition or not. The output will be false if even one value does not sati
3 min read
What is the difference between find() and filter() methods in JavaScript ?
In this article, we will see the difference between the find() and filter() methods in javascript. JavaScript find() method: The find() method is used to find all the descendant elements of the selected element. It finds the element in the DOM tree by traversing through the root to the leaf. Syntax:
2 min read
Difference between Set.clear() and Set.delete() Methods in JavaScript
Both Set.clear() and Set.delete() methods modify the present Set by removing elements. But there are some fundamental differences between these two, which will be discussed in this article. Set.clear() Method: In this method, all the elements in the set get deleted. So, as a result, the set becomes
2 min read
Difference between Object.keys() and Object.entries() methods in JavaScript
Object.keys() and Object.entries() are methods in JavaScript used to iterate over the properties of an object. They differ in how they provide access to object properties: Object.keys() returns an array of a given object's own enumerable property names, while Object.entries() returns an array of a g
2 min read
How to wait for multiple Promises in JavaScript ?
Waiting for multiple promises in JavaScript involves using Promise.all() or Promise.allSettled() methods. These methods accept an array of promises as input. Promise.all() waits for all promises to either resolve or reject, providing a single promise that resolves with an array of results or rejects
3 min read
How to call promise inside another promise in JavaScript ?
In JavaScript, to call promise inside another promise we first declare a promise using its basic syntax and further execute this promise then create another which is to be called inside the previous promise for its execution. Promise is basically a JavaScript object which is responsible for handling
3 min read