Transcript
00:00 Alright. Let's play around with this. So we've got our user, and then we have a user profile that includes a status of whether it was successful or not and an error message if it was not successful. Our fetch user has been updated to say if the ID that is given to, for the user to fetch, if that's not equal to one, then that user has not been found. We only have one user in our database, evidently.
00:21 And then we resolve if the ID is one. Okay. So great. Let's go ahead and just call get user profile with one and we'll console log the result just to make sure all of that is working. And, yep, it works just fine.
00:35 So that's great. Then we go ahead and do two. Tada. And what happens? Nothing happens because our then handler was not called.
00:46 So then is only called when, things go well. If things did not go well, then your then is not called. Instead, you're going to, actually have a couple of options of how to deal with this. So one option is you add a catch handler. So that's what our our AI assistant is trying to do here is we're going to update to handle errors, and that's that's working right here.
01:12 So if the user is found successfully, then we'll return a status of success, and here's the user. If that, fetch user fails, then it's gonna fall into this catch category, where we get the error and we can then return the error message. And so then, we're our promise is now properly handled. And, the promise that's returned from this catch call because this is chaining, so we got promise. We create we create a promise with this.
01:40 We create a promise with this, and then we actually create another promise with this. So because this is handled, now, the promise that's returned from here, is in, encapsulated in this call. Then we will call a then on that. And because it's handled properly, we get the result, which is going to be our error in this case. So a couple of interesting things about this.
02:05 You could actually go really, really deep and really far into how all these promises chain and everything, and I encourage you to do that. But there, there are a lot of things to consider with this. First, before we go too far into that though, I wanna show you one other, mechanism for handling this error, and that is actually a second argument to then. So then it's actually accepts two arguments. So this is one.
02:30 This is the the callback if things go well, but you can also provide a callback if things go poorly. So I can get rid of this catch call, and we can do, here's what happens if things go well, and here's what happens if things don't go well with the fetch user callback. K? So we actually end up with the exact same behavior. So these are effectively the same thing, but they are slightly different, and I'm gonna show you how.
02:57 So here, we're going to, yeah, return success, etcetera, etcetera. But what if we're doing something inside of here? And then, yeah, we can do as const, I suppose. What if we're doing something inside of here that, blows up? Like, we say console log user, say hi, and we're not using TypeScript or something, and this blows up.
03:18 Well, we're going to end up oh, here. And let's fix this right there. We're going to end up calling this. It blows up, and we're not handling that. And the reason that is happening is because, this error handler right here is handling errors with the fetch user.
03:34 It's not handling errors with our promise here. K? So we are when we create this then right here, this creates a new promise. This error handler, the second argument to this then, is handling errors on this promise, not the one that's being created with it. And so we could actually catch right here and we're gonna say, holistic error to distinguish it from others.
04:02 There we go. So there we go. We get, say hi is not a function. Here's our holistic error. So that works out nicely for us.
04:12 But that's what's going on right here is this error handler is only gonna handle errors on the promise upon which it was added. So that's one reason why you might prefer to use a dot catch, because in this case, we are going to properly handle it. We'll get the error. We'll get the message. It's not going to blow up in our faces where we end up having to catch it elsewhere.
04:34 So, that's an important thing to to recognize. The other thing is sometimes you might have a catch handler or even, an error handler here where you want to, catch that error but also propagate it. So, like, have it continue on. So we're gonna catch it so we can add a logging, but then we want somebody else to handle it for us. And so if that is the case, then, we can still return or or or, we can still maybe let's say that we do wanna console log it.
05:04 So we're gonna console dot or we'll console error that. There we go. But then we're going to want to reject or or maintain the rejected status of the promise. So if we simply return something right here, that means we're turning the error that was thrown, by this promise. We're turning that from a rejected status into a resolved status because we're handling it and we're doing something with it.
05:26 But if we wanna maintain that rejected status, then there are a couple ways we could do it. We could say throw error. So that'll just take the exact same error and propagate it up, make somebody else deal with it. And so here, we're gonna see we get that console law or that console error. So that's gonna show us that error here.
05:43 And we're going to propagate the error so somebody else handles it and that's where we're handling it right here. So, the other thing to consider here is what if we wanted to change that error somehow? Well, you could either throw a new error, and you can have unknown errors or something, or you can actually return a rejected promise. So you can actually create resolved and rejected promises with promise dot resolve and promise dot reject. And if we return a rejected promise, then that will maintain the rejected state, and propagate it up still.
06:19 So it's similar to, saying throw. So you could say throw right there or you could say return rejected promise and here is the thing I'm rejecting. Now this is the the error that we're gonna get right here. So, yeah, there are a whole bunch of ways to mix and match these. I encourage you to take some time, play around in here, try to throw an error, try to do a promise dot reject, do a promise.resolve, try to move this callback inside of, as the second argument here, play around with where all of the code is going, stick debug statements and console logs, all of that stuff.
06:54 Explore plenty with this to kind of get a good idea of how all of the code flows in here. And with that, I think, you have learned what you need to know about promises. So good job on that one.
