DEV Community

Discussion on: What's wrong with Promise.allSettled() and Promise.any()❓

Collapse
 
ressom profile image
Jason Mosser

allSettled is useful in at least two principle cases. First, when calling multiple operations that are recoverable on failure. Second, calling multiple operations where 'silent' behavior is desired.

It avoids using errors for flow control, which is good.

const [r1, r2] = await Promise.allSettled([op1(), op2()]);

// decode state of the operations and retry if needed //
// or, if one does not care about the results, just invoke it and move on //
Collapse
 
vitalets profile image
Vitaliy Potapov • Edited

I totally agree - this is useful scenario.
My point is that it can be easily achieved with current api without introducing new methods:

const [r1, r2] = await Promise.all([op1(), op2()].map(p => p.catch(e => e)));

// decode state of operations by simply checking "instanceof Error".
Collapse
 
itscodingthing profile image
Bhanu Pratap Singh

I think it is more about the complexity or how much code you write to achieve same functionality without worrying about the other things.