Check out a free preview of the full Advanced Web Development Quiz course

The "Q18: Promise Methods" Lesson is part of the full, Advanced Web Development Quiz course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to match each Promise method with the correct output. The Promise methods used are all, any, race, and allSettled.

Preview
Close

Transcript from the "Q18: Promise Methods" Lesson

[00:00:00]
>> Connect the Promise methods to the right output. So, we have all, race, any, and allSettled, and then we have Result: 2, Error: 3, Result, which is an array of objects. These aren't supposed to be empty objects by the way but I just couldn't fit it all in there, and 4 is also a Result: 2.

[00:00:16]
So there are two result twos, which is, yeah. Anyway, connect what should be in that little method box to get that result, or yeah, get that output. Hope the question phrasing [LAUGH] makes sense, kind of. So for all, it'll be Error 3, race is Result: 2, any is also Result: 2, and allSettled will result or will have to result with the array of objects.

[00:00:43]
So first, let's just take a look at what does promise.all do. So with this method, it kind of waits for all the promises in the array to settle. So either resolve or reject, and then resolve with an array of the result values. But if any of the Promises rejects, it will immediately reject with the first rejection.

[00:01:03]
So here, the first one fulfills them or resolves them all. So it just fulfills with the array with the values. The second one or like the one right below, it rejects with that first rejection. And the third one, like on top, I don't know if it's third, it will also reject that the first rejected Promise.

[00:01:19]
Same for the fourth. So in the case of our Promises, this is kind of like the code that reflected as the lines. So the first rejection, which is Error 3, will get returned from Promise.all. So Promise.all is also Promise that then rejects with that rejected value. Next, we have Promise.race, which waits for the first promise in the array to settle.

[00:01:44]
So again, either resolve or reject. So it just depends on which Promise is first. If the first Promise resolves, then it will fulfill, but if the first Promise rejects, then it'll also get rejected. So this is just the first Promise they well, as the name suggests, they race.

[00:02:01]
It's just the first Promise that resolves. Yeah, so in this case, even though we have two, we have a Promise that resolves and one that rejects. Then it just depends on which in real life is never quite the case. This was just like a set timeout. But in that case, just depends on which Promise was defined first, and in this case, that was the resolving one so it'll be fulfilled with Result: 2.

[00:02:25]
Then we have Promise.any, which wait for the first promise in the array to resolve. So if a Promise rejects, it'll just ignore that one. It's waiting for the first one to resolve. And if none of them resolve, so the second one, it'll have this aggregate error, which is just an array of all the rejected reasons, but only then will it reject, otherwise, it'll always fulfill.

[00:02:49]
Yeah, so in this case, again, it's just the first resolving Promise, which is Result: 2. And then allSettled we'll never reject it, it will always just return an array with a status of either fulfilled or rejected based on what that Promise either if it fulfilled or rejected. And then the reason for the rejection if it was rejected or the value of that fulfilled resolved Promise.

[00:03:13]
So yeah, even if they all just rejected, it will still be fulfilled. So this is pretty useful if the API calls or if you're using this for API calls and they aren't really depending on each other. And maybe one failed and you just wanna retry it without having to catch or you still want the other Promises or other API calls to finish as well.

[00:03:34]
Then you can just easily retry. Maybe if status is rejected in one of those, then retry it for that API call. But for the other ones, just keep going. So yeah, that, right, so for our Promises that will just return this array with a fulfilled, fulfilled, rejected, and fulfilled.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now