Manage Asynchronous JavaScript code with any, allSettled new Promise Methods

Tomasz Ducin
InstructorTomasz Ducin
Share this video with your friends

Social Share Links

Send Tweet

Promise.any takes an iterable of promises and returns a promise that fulfills with the first promise that fulfills, and rejects when ALL of the promises in the iterable reject.

Promise.allSettled takes an iterable of promises, and returns an array of objects. The objects have a status property which is a string that is either "fulfilled" or "rejected". If the status is "fulfilled" the object will contain a value property with the resolved value of the promise, and if the status is "rejected" the object will have a reason property with the reason for the promise's rejection.

[00:00] Create a new promise using the static methods of the promise constructor. In this case, promise dot any. It accepts an iterable of promises which most of the times is going to be simply an array of promises. And let's pass a promise that is immediately fulfilled with an [00:19] example value 125, and let's await the promise aggregate any. So promise dot any is waiting for any of the promises to fulfill. So waiting for the first one to fulfill. Let's also create another promise, which is going to be [00:39] promise dot all settled, which we're going to pass again and iterable of promises and promise dot resolve with 125. So this one is going to resolve when all of the promises passed as the array parameters will [00:59] settle. So no matter what is their status, whether they are fulfilled or rejected. So if we have immediately resolved or rejected promises, we won't see the differences in how they work. So let's introduce a utility functions. So the fulfill function is going to create a [01:18] promise which will fulfill after a time out of after some time has passed, And it's going to fulfill with the value that we pass over here. And the rejected function is going to create a new promise that would reject again after some time has passed with the reason that has been [01:38] passed as the parameter. So let's see how does both fulfill and reject work in practice. So let's pass the success string into the fulfill. And let's fulfill the promise after one second. So after one second has passed, we're expecting a [01:58] wait to grab and show what is the success value of the fulfilled promise. And again, await, reject, and let's pass kaboom string after 2 seconds. So we're expecting a string to be thrown after exactly 2 seconds, and this is what we [02:18] get. So let's also create the const. I promise 1p 1, which is again the fulfilled promise. And let's create also the const p 2, which is a newly rejected promise. And let's see how do different [02:38] promise aggregates work. So when we use the promise dot all, it means that this one will fulfill when all of these fulfill. So in our case, the first promise will fulfill. The second will reject. So the whole thing will reject with the reason of the [02:58] first promise that has failed. So the first to fail will also fail the promise dot all aggregate. So the reason for the first failure is also the reason for the failure of the whole aggregate. The other aggregate that we have known for a couple of years was promised not race, [03:18] which is going to figure out the first one to settle. So it could also be called first settled. So the first settled by the meaning of the passage of time would be the first fulfillment of p one. So this is basically going to be success only because it was the [03:38] first one. So the new aggregates, in this case, any, this is going to wait for the first to fulfill. So in this case, this is going to be a success after one second. However, if we switched what is the order over here, so the first one to settle at all would be kaboom, but we're [03:58] waiting for the 1st to succeed. And after 2 seconds, it does take place. So this was promised dot any and promised dot all settled is going to wait for all of the promises to settle no matter what is their status. So once all of them settle, we're going to [04:17] get both the status and the value or the reason, depending whether a promise has been fulfilled or rejected. So promise dot all settled might not fulfill if any of the promises remain pending forever.