1 00:00:01,210 --> 00:00:03,100 We spent all this time on air handling. 2 00:00:03,100 --> 00:00:08,560 So now surely there is no way that we could break everything by just adding in one keyword. 3 00:00:08,620 --> 00:00:09,500 Right. 4 00:00:09,520 --> 00:00:11,190 There's no way that could possibly happen. 5 00:00:11,550 --> 00:00:12,220 Oh whoops. 6 00:00:12,280 --> 00:00:12,710 Yeah. 7 00:00:12,730 --> 00:00:17,440 Turns out we can break all the error handling stuff we just did by adding in one very small keyword. 8 00:00:17,440 --> 00:00:19,840 So in this video we can take a look at what that keyword is. 9 00:00:19,840 --> 00:00:23,500 We're gonna understand what it does to us and how we're gonna solve it and make it work with our area 10 00:00:23,500 --> 00:00:25,010 handling approach. 11 00:00:25,070 --> 00:00:29,830 Gonna find that little apt on all statement we just added in a moment ago and I'm gonna put it on the 12 00:00:29,890 --> 00:00:34,690 async keyword like so now surely that's not going to do anything to us right. 13 00:00:34,720 --> 00:00:37,610 We don't actually have any await syntax inside of your. 14 00:00:37,750 --> 00:00:39,670 All we're doing is throwing in an error. 15 00:00:39,700 --> 00:00:41,640 Well let's save this. 16 00:00:41,800 --> 00:00:49,180 Go back over to post man and make that same request once again Hey wait a minute it's stuck in sending 17 00:00:49,180 --> 00:00:49,870 request. 18 00:00:50,170 --> 00:00:51,340 What's going on here. 19 00:00:51,340 --> 00:00:57,850 How did we just break this by adding on that one little keyword Well you've got to understand what async 20 00:00:57,850 --> 00:00:59,070 does behind the scenes. 21 00:00:59,160 --> 00:01:04,480 Never you mark a function as async that function is no longer going to immediately return any value. 22 00:01:04,510 --> 00:01:09,280 Instead it's going to return a promise that's going to resolve with some value in the future even if 23 00:01:09,280 --> 00:01:11,170 we throw in error inside there. 24 00:01:11,230 --> 00:01:12,820 How does this relate to express. 25 00:01:13,180 --> 00:01:16,900 Well let's go back to the Express error handling documentation once again. 26 00:01:16,900 --> 00:01:22,540 You might recall that I had mentioned very briefly that if we had a synchronous root handler and we 27 00:01:22,540 --> 00:01:28,030 threw an Air Express will automatically capture that thing and throw it off to any error handling middleware 28 00:01:28,030 --> 00:01:30,400 we have to find inside of our application. 29 00:01:30,400 --> 00:01:37,120 However if we have an asynchronous middleware or an asynchronous root handler and when we say asynchronous 30 00:01:37,150 --> 00:01:44,350 we are talking about any root handler that's going to have a secured applied to it a callback or a promise. 31 00:01:44,350 --> 00:01:50,220 Then we have to rely upon this next function instead and that's what we just did. 32 00:01:50,220 --> 00:01:52,570 We now have an asynchronous root handler. 33 00:01:52,580 --> 00:01:58,620 So if you want this thing to work correctly we have to receive that next function. 34 00:01:58,880 --> 00:02:02,440 And when we decide to throw in error we don't actually throw it. 35 00:02:02,450 --> 00:02:14,610 Instead we will call next with our air let's now save this look back over to man and try this again. 36 00:02:14,670 --> 00:02:16,800 Looks like it's working once again. 37 00:02:16,920 --> 00:02:22,860 Well obviously rather than doing the throw new air versus next I would rather not have to worry about 38 00:02:22,860 --> 00:02:24,210 this next thing at all. 39 00:02:24,240 --> 00:02:28,500 It's something it's very particular to express and it really requires engineers to understand what next 40 00:02:28,500 --> 00:02:32,910 is doing behind the scenes I think would be a lot better if we could somehow figure out a way to stick 41 00:02:32,910 --> 00:02:36,440 with the throw keyword instead and just make this work. 42 00:02:37,660 --> 00:02:42,520 Well to do so we're going to install a little package a little package that's going to change the default 43 00:02:42,520 --> 00:02:47,370 behavior of how express handles root handlers like the one we just put together right here. 44 00:02:47,500 --> 00:02:52,600 This little package is going to make sure that express waits or essentially does in a weight statement 45 00:02:53,080 --> 00:02:54,730 on this function. 46 00:02:54,850 --> 00:02:59,710 And so if we have some async code inside of here for example using async await express we'll start to 47 00:02:59,770 --> 00:03:05,050 watch for any errors that we throw at any point time inside this function but essentially it's just 48 00:03:05,110 --> 00:03:10,450 augmenting the default behavior of Express and kind of bringing it into the present age of using async 49 00:03:10,510 --> 00:03:11,380 await. 50 00:03:11,500 --> 00:03:14,240 This package is extremely easy to use. 51 00:03:14,260 --> 00:03:16,180 Let's just go take a look at the documentation for it. 52 00:03:16,180 --> 00:03:25,690 Very quickly I'm going to open up NPM the NPM J.S. dot com and at the top I'm going to do a search. 53 00:03:25,700 --> 00:03:30,920 We're going to look for I do not recall the name of it off the top I had a gotta check my notes really 54 00:03:30,920 --> 00:03:31,500 quickly. 55 00:03:31,520 --> 00:03:32,090 There we go. 56 00:03:32,090 --> 00:03:36,950 Turns out there's a dozen packages with very similar name which is why I didn't really just want to 57 00:03:36,950 --> 00:03:40,850 randomly search for it so express async errors. 58 00:03:40,850 --> 00:03:43,430 That's what we're looking for. 59 00:03:43,480 --> 00:03:48,550 So here's express async errors you can read the documentation here if you want to. 60 00:03:48,600 --> 00:03:50,830 It pretty much just does what I mentioned. 61 00:03:50,880 --> 00:03:56,040 It's going to make sure that if we ever throw an error at some point time inside of an async function 62 00:03:56,700 --> 00:04:00,080 this whole package is going to make sure that express listens for it. 63 00:04:00,180 --> 00:04:00,720 Use this thing. 64 00:04:00,720 --> 00:04:07,440 All we have to do is install it and then require it in or in our case import it in right after Express 65 00:04:07,830 --> 00:04:08,190 that's it. 66 00:04:08,220 --> 00:04:13,060 That's all we have to do so let's go over to our terminal and install it into our project 67 00:04:15,790 --> 00:04:17,480 inside of our ticketing directory. 68 00:04:17,500 --> 00:04:25,490 I'll change into off and do an npm install express async errors. 69 00:04:25,530 --> 00:04:31,040 This is a very small module so it should install rather quickly after installing it all then go backwards 70 00:04:31,040 --> 00:04:36,230 my index start to yes file a go up to the top I'll find where we import express and then right after 71 00:04:36,230 --> 00:04:45,980 that I'll add an import express async errors and that should be at I'll save this. 72 00:04:46,050 --> 00:04:49,840 You'll notice that I still have code that was causing us trouble a moment ago. 73 00:04:50,190 --> 00:04:54,840 But if I go backward a postman and send a request once again. 74 00:04:55,080 --> 00:05:00,360 Now it's working and even though we've got that async keyword in there. 75 00:05:00,480 --> 00:05:04,710 So this is going to fix up not only the root handlers we define inside this file but inside of other 76 00:05:04,710 --> 00:05:05,910 files as well. 77 00:05:05,910 --> 00:05:13,530 So for example if we go back over to sign up here's the sign up file find the actual root handling function 78 00:05:13,560 --> 00:05:16,020 and put the async keyword on there as well. 79 00:05:16,750 --> 00:05:22,160 Now we can go backward a postman I'll fix up my URL so going to delete all that gibberish at the end. 80 00:05:23,030 --> 00:05:25,170 I'll make sure I'm still making a post request. 81 00:05:25,490 --> 00:05:26,640 Now if I send this off. 82 00:05:26,800 --> 00:05:30,690 Yeah this works as well. 83 00:05:30,710 --> 00:05:31,940 Well I'd say this looks pretty good. 84 00:05:31,940 --> 00:05:36,590 Now we can handle errors across the board and I'm pretty sure that really actually is the last error 85 00:05:36,590 --> 00:05:40,010 related topic we need to worry about for a long time. 86 00:05:40,010 --> 00:05:46,400 So with that in mind because right here in a start taking a look at actual authentication is what we 87 00:05:46,400 --> 00:05:49,130 actually originally set out to do in just a moment.