Promise.race() vs Promise.any()

Nihal Mahesh
2 min readJun 4, 2021

So Promise.any() has been released in ES2021 both does the same thing but there is a minute difference between the both. So we can understand those differences here.

Javascript

So Javascript follows 3 stages whenever a promise has been called: Pending, Fulfilled, Rejected.

Promise.race()

It returns a promise either that is in fulfilled stage or rejected stage. It just return the first promise thats it. It doesn’t care about whether that promise is getting rejected or fulfilled.

The console.log value is Getting Rejected as the promise1 is getting rejected in 2 seconds(2000 ms). So the value is Getting Rejected.

Promise.any()

It returns a single promise only when the promise in in fulfilled stage . Only the first fulfilled promises will be coming out of Promise.any()

As we can see here the output is logging Getting resolved. Even the promise1 gets completed first but it will not take into account as that promise is getting rejected so the next promise will resolve at 4000ms. So Getting Resolved will be getting logged.

Promise.any() is the opposite of Promise.all(). You can refer it here

So we got to know the major difference between these 2 Promise. Till then Happy Coding.

--

--