Promise allSettled vs Promise all
Promise all
1
2
3
4
5
6
7
8
Promise.all([
Promise.reject('✗'),
Promise.reject('✗'),
Promise.resolve('✓'),
Promise.reject('✗'),
]).catch((err) => {
console.log('You win at life', err);
})
Promise all
will reject as soon as there is a Promise in the Array returned to reject
.
Result:
1
You win at life ✗
Promise allSettled
1
2
3
4
5
6
7
8
Promise.allSettled([
Promise.reject('✗'),
Promise.reject('✗'),
Promise.resolve('✓'),
Promise.reject('✗'),
]).then(function(value) {
console.log(`You win at life`, value)
})
Promise allSettled
will run all Promises in the array, regardless of whether they return rejected
or fulfilled
Result:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
You win at life,
[
{
status:"rejected",
reason:"✗"
},
{
status:"rejected",
reason:"✗"
},
{
status:"fulfilled",
value:"✓"
},
{
status:"rejected",
reason:"✗"
}
]
This post is licensed under CC BY 4.0 by the author.