DEV Community

Cover image for 🤝 Promise.allSettled() VS Promise.all() in JavaScript 🍭

🤝 Promise.allSettled() VS Promise.all() in JavaScript 🍭

Victor de la Fouchardière on August 16, 2020

Hello ! 🧑‍🌾 Promises are available since ES2015 to simplify the handling of asynchronous operations. Let's discover 2 Promises and their differe...
Collapse
 
pankajpatel profile image
Pankaj Patel

This is a really handy, allSettled has more verbose output

Thanks for sharing @viclafouch .

Collapse
 
viclafouch profile image
Victor de la Fouchardière

Youre welcome

Collapse
 
iarmankhan profile image
Arman Khan

Loved the article

Collapse
 
viclafouch profile image
Victor de la Fouchardière

Thank you @iarmankhan ;)

Collapse
 
shoaibbagdadi profile image
Suyeb Bagdadi • Edited

You can as well do the following to stop Promise.all from rejecting if there is an exception thrown.``

`
let storage = {
updated: 0,
published: 0,
error: 0,
};

let p1 = async (name) => {
let status = {
success: true,
error: false,
};
return status;
};

let p2 = async (name) => {
throw new Error('on purpose');
};

let success = () => {
storage.updated += 1;
};

let logError = (error) => {
console.log(error.message);
storage.error += 1;
};

Promise.all([
p1('shobe 1').then(success).catch(logError),
p2('shobe 2').then(success).catch(logError),
p1('shobe 1').then(success).catch(logError),
]).then(() => {
console.log('done');
});

`

Collapse
 
syeutyu profile image
Dayzen

Thanks for sharing this post!

Collapse
 
polluterofminds profile image
Justin Hunter

Whoa! I had no idea this existed. Thanks for the helpful write-up!

Collapse
 
viclafouch profile image
Victor de la Fouchardière

A pleasure @polluterofminds ;)

Collapse
 
devinrhode2 profile image
Devin Rhode

I'd love some elaboration on why allSettled was made/why it's better

Collapse
 
devinrhode2 profile image
Devin Rhode
Collapse
 
yogendra3236 profile image
Yogendra

How can I use Promise.allSettled() with my webpack-react app? Is there any plugin being used for it?

Collapse
 
mohdaliyan profile image
Mohd Aliyan

Very well explained. Thank you so much Victor.

Collapse
 
vguleaev profile image
Vladislav Guleaev

short and nice!

Collapse
 
cruzblaugrana profile image
Shakhruz

Helpful bro, thnx !!!