YTread Logo
YTread Logo

Promises and the allSettled Method

Mar 16, 2024
Alright, today I want to talk about promise, all figured out, this is a relatively new data

method

for

promises

. If we look at the data we have here, can I use it? We can see that the dates that browsers added this functionality were for the most part in 2019, so it's been around for about a year, how does this work well to give some context? I want to talk about promisol and the promise race real quick. I have created four

promises

here, the first two of which p1 and p2 one is going to be resolved immediately, one is going to be resolved after 100 milliseconds, so in a tenth of a second the values ​​they are going to return are one and two and then my other two promises will return three and four, but they are rejecting, so we have two that are working, two that are failing.
promises and the allsettled method
Now, to start, let's take a look at the running

method

. When this is run for the race, we go through a series of four different promises and the race wants to know which of them will come. Come back first, so if we look at these we can see that two of them are going to be resolved and that's actually what's going to happen here right now because this is the first one that's being resolved right away. We'll probably get this as a response, so in race condition number one it's probably going to win, it was written first in my code and there's no real delay, so it was added to the stack on the side to be returned first.
promises and the allsettled method

More Interesting Facts About,

promises and the allsettled method...

We should get that now, everything tells us when four come back if all four were good, so the promise race is the first of the four answers that was resolved, it wasn't rejected, but the first one before that was resolved, the promise is that they all resolved, so if I run this right, we get the first of the four responses are great, we can even look to see well in my response the state or here let's see the response object itself, we'll just put it here, like this let's look at that, run this again, there we go, so we'll get the response that was resolved. number one is what came back, so we know it was this first one here that worked out well, just enough that it worked for the race.
promises and the allsettled method
The promise didn't work because not all four came back as valid, two of them were resolved. two of them failed so we will get the error each time and it gives us here error number three which is the first one that was rejected so we got number one, number two but number three was the first one that was rejected , In fact. It will probably give us number one and then number three right after, so that this one will be rejected and that's why we're putting number three here now. If we put a timeout set here, we would simply create a delay in this first resolution again.
promises and the allsettled method
We're going to give it the same amount that we're going to say after a tenth of a second, so now when we run this, come on, they're both going to fail the race, the first thing that's going to come back is this number three. because it is the only one without a timeout, so this is the one that responds immediately. The first thing that came back in the race failed, so that doesn't help us much. We have some that failed. Now the new, the new method. We have the promise point everything resolved now this is for situations like the one we are doing here we pass a series of promises so p1 p2 p3 p4 and we know that there is a possibility that some fail and others work. to have my then in capture same as before in my, so what I'm going to get is an array that has all of these responses, so let's say I'm just going to put it in a variable called responses and set up our function. for the capture I'm going to do the same thing I did here, there we go, so in the capture we're going to write what the error is if there was something that failed, but there's something a little different here. is happening with everything resolved and that is we are asking for all responses regardless of whether they were rejected or resolved so if it worked or not I want to know when they all finished so now rejected promises are not triggered. the capture instead, this just waits for all of them and then goes down and calls this, so inside here we're done with all four of them, regardless of whether they worked or not, so let's clear it up and run this again there, we can see it finished. the four of us didn't get the trick we know two of them were failing two of them were rejected but we're not getting the trick we can work with all the answers now I can take a look at each of those, so we can say answers, it's a matrix , so I'll use four each.
I'm going to go through them all looking at each of the answers individually, so I have my index number here as well now inside. from here we will just do console.log and write the index number and our response object, so these will be each one of them individually and there we end up with the four, the four were. we go back to the first one regardless of whether they were kicked out or resolved and we can loop through them and look at each one of them, so here is the response object for each one of them, we will get a status for each one of them and it will tell us fulfilled or rejected and pending will not be a possible value here because pending is only active until it is called, so after it is called we have only fulfilled and rejected, those are the only two possible states, so if it is fulfilled we get a value and if it is rejected we get a reason I mean it's a value for all four but the property is actually called reason so we can check the state which we can do if the statements here check the state and decide what I want to do maybe two of these things we absolutely had to have and then two of them were fine, if they work great I'll use them if they don't work well I can get away with not having the values ​​for those promises and I'll continue to make the rest of my page work well , so I hope it helps you.
If you have any questions, feel free to leave them in the comments. If you found it useful, please share it and, as always, thanks for watching.

If you have any copyright issue, please Contact