DEV Community

Discussion on: New Features in ECMAScript 2021 (with code examples)

Collapse
 
tarektouati profile image
Tarek Touati

Great Post !

Can you please explain the difference between
Promise.race and Promise.any ?
They booth act the same ? Does Promise.race have been deprecated ?

Collapse
 
faithfulojebiyi profile image
Faithful Ojebiyi

Promise.race() isn't deprecated either.. They have different use cases

Collapse
 
faithfulojebiyi profile image
Faithful Ojebiyi

They are a bit different
Promise.race is resolved as soon as any of the promises you feed it resolves, whether they are fulfilled or rejected. Promise.any is settled as soon as any of the promises you feed it is fulfilled or all the promises are rejected. If all promises are rejected it returns an AggregateError.

So Promise.race returns a promise that is settled regardless it being resolved or rejected. But promise.any will not return if one promise is rejected. It will continue until the first resolved promise. If in any case none of the promises resolves then it returns an AggregateError.
I hope I cleared your confusion