1
00:00:00,870 --> 00:00:06,910
And up next, I want to work on a cigarette, rappers for all our controllers.

2
00:00:06,960 --> 00:00:07,330
Why?

3
00:00:07,950 --> 00:00:15,180
Well, because we have asynchronous operations and yes, it's very useful to use these try catch blocks.

4
00:00:15,660 --> 00:00:22,920
But you have to agree with me that he had become somewhat redundant pretty much for every controller

5
00:00:23,160 --> 00:00:23,670
setting up.

6
00:00:23,680 --> 00:00:25,950
Try and then catch on.

7
00:00:25,950 --> 00:00:29,310
As you can see, essentially, I'm just copying pasting the code around.

8
00:00:29,580 --> 00:00:31,470
So there has to be a better solution.

9
00:00:31,810 --> 00:00:40,650
A better solution is to create a middleware function that essentially will wrap our controllers in there,

10
00:00:40,650 --> 00:00:44,550
will disrupt functionality where we don't have to repeat ourselves.

11
00:00:44,730 --> 00:00:51,240
And before we continue, let me just say that there are some NBN packages that do that for us and most

12
00:00:51,240 --> 00:00:57,120
likely in the upcoming project will use them instead, since that way we can save some time on a set

13
00:00:57,120 --> 00:00:57,260
up.

14
00:00:57,720 --> 00:01:03,870
But I think it's very important to have at least a general idea of what the package is doing instead

15
00:01:03,870 --> 00:01:05,220
of blindly installing it.

16
00:01:05,550 --> 00:01:10,320
And therefore, in our first project will set up the basic logic ourselves.

17
00:01:10,920 --> 00:01:15,570
And I'll start over here with new file and I'll call this async jurors.

18
00:01:15,870 --> 00:01:23,340
And then inside of it, I'm just going to go const a sink or wrapper so I simply create a function.

19
00:01:23,880 --> 00:01:27,390
At the moment, nothing's happening here and then I want to export it.

20
00:01:27,930 --> 00:01:34,170
Module exports and then a sink wrapper and once we set up the export.

21
00:01:34,560 --> 00:01:38,970
Now, of course, I just want to go to my comptrollers and import that.

22
00:01:38,970 --> 00:01:45,160
Magloire So I'm looking in the middle and I'm looking for a sink in here.

23
00:01:45,270 --> 00:01:48,870
Let's just call this a sink wrapper again.

24
00:01:49,080 --> 00:01:54,720
And that is equal to require that we navigate to our middle order folder.

25
00:01:54,990 --> 00:01:57,180
And of course, we're looking for your sink.

26
00:01:57,630 --> 00:02:03,030
And now what we want to do is wrap all of our comptrollers.

27
00:02:03,330 --> 00:02:05,360
Now, I'm not going to wrap all of them right away.

28
00:02:05,610 --> 00:02:11,230
I'll just start with get all tasks just so I understand how it is going to work.

29
00:02:11,520 --> 00:02:18,840
So what do we want to do here is just to take this async wrapper list function and just wrap our current

30
00:02:18,840 --> 00:02:26,340
controller and essentially is going to look like this where we go with a sink or wrapper and we pass

31
00:02:26,340 --> 00:02:30,600
in the controller, the current controller as an argument.

32
00:02:30,840 --> 00:02:34,560
And since we know that we can do that in JavaScript, we are in good shape.

33
00:02:34,830 --> 00:02:41,550
So we pass it in and then back in the sink, of course, and it set up the parameter.

34
00:02:41,940 --> 00:02:49,260
So since I'm passing in already in the controllers, my function, my controller has an argument back

35
00:02:49,260 --> 00:02:51,750
in my singes, my middleware.

36
00:02:51,990 --> 00:02:54,070
I just need to go with some kind of parameter.

37
00:02:54,360 --> 00:02:55,830
Now, are you going to call this callback?

38
00:02:55,860 --> 00:02:57,000
You can call this function.

39
00:02:57,360 --> 00:02:58,220
Doesn't really matter.

40
00:02:58,230 --> 00:02:58,730
In my case.

41
00:02:58,740 --> 00:03:00,630
I'm just going to go with F and.

42
00:03:01,110 --> 00:03:03,010
And what am I trying to do here?

43
00:03:03,450 --> 00:03:07,190
Well, I'm trying to avoid this try catch.

44
00:03:07,410 --> 00:03:12,600
So essentially I want to have my cake and eat it too, where I can still use this.

45
00:03:12,600 --> 00:03:18,360
And I await syntax, but I don't have to set up the try catch block.

46
00:03:18,630 --> 00:03:19,660
How can we do that?

47
00:03:19,860 --> 00:03:26,520
Well, we set up the tri box inside of the wrapper and the first thing that I want to do from this function

48
00:03:26,820 --> 00:03:31,800
is to return the one screen here also return async.

49
00:03:32,160 --> 00:03:41,190
And then if you remember in the express by default, we right away it got request response as well as

50
00:03:41,190 --> 00:03:41,590
the next.

51
00:03:41,970 --> 00:03:48,110
So that's what I'm trying to do over here since I wrapped my controller in my middleware.

52
00:03:48,330 --> 00:03:54,140
And if you pay close attention, we're actually invoking the async wrapper right away.

53
00:03:54,390 --> 00:04:01,860
Now, of course, I want to pass in those requests response and next down to my function down to this

54
00:04:01,860 --> 00:04:02,400
controller.

55
00:04:02,610 --> 00:04:03,050
Correct.

56
00:04:03,270 --> 00:04:06,840
Because now, of course, I wrap my controller in my Millau.

57
00:04:07,170 --> 00:04:13,280
And the way we do that, we return another function since I will use a wait inside of the function body.

58
00:04:13,530 --> 00:04:15,570
That's why I set it up here as a sync.

59
00:04:15,780 --> 00:04:22,740
And please, please, please keep something in mind where these ones, the Iraq rise and next we'll

60
00:04:22,740 --> 00:04:29,310
have access to right away, because at the end of the day, we return another function from async wrapper.

61
00:04:29,490 --> 00:04:36,780
And that inside of the function by one of the things that I want to do is pass the Iraq address and

62
00:04:36,780 --> 00:04:40,620
next from express down to this function.

63
00:04:40,980 --> 00:04:42,120
So hopefully that is clear.

64
00:04:42,130 --> 00:04:44,310
Again, we get these ones by default.

65
00:04:44,550 --> 00:04:50,400
The reason why we have access to them, because from our async wrapper, we actually return another

66
00:04:50,400 --> 00:04:50,900
function.

67
00:04:51,270 --> 00:04:53,880
So that's how we will have right away access to them.

68
00:04:54,180 --> 00:04:58,860
And that is the function body will decide what to do with our parameter.

69
00:04:59,160 --> 00:04:59,640
We.

70
00:04:59,950 --> 00:05:06,820
Argument, which is our current comptroller and as far as logic, I want to go with try catch block

71
00:05:07,180 --> 00:05:14,230
and then instead of the tribe block, I'm going to go with a wait and function and herel person, Rick

72
00:05:14,620 --> 00:05:16,590
or and I'm next.

73
00:05:16,990 --> 00:05:22,990
And then once we have passed all of these things down to our controller, of course, now have access

74
00:05:22,990 --> 00:05:24,850
to them in here as well.

75
00:05:25,000 --> 00:05:31,050
And then also, since there might be a case for an error, we'll go with next and then we'll pass in

76
00:05:31,060 --> 00:05:31,420
here.

77
00:05:31,600 --> 00:05:37,090
And if you're wondering what's happening over here, essentially we're passing this to a next maneuver,

78
00:05:37,660 --> 00:05:40,480
which we haven't set up yet in our abcess.

79
00:05:40,900 --> 00:05:46,300
So that's what's coming up in the following videos, we'll set up another set of middleware.

80
00:05:46,460 --> 00:05:49,110
And in this case, we will deal with errors.

81
00:05:49,420 --> 00:05:57,160
So once we save over here, we can go back to the controllers and now I can remove the try, catch block.

82
00:05:57,520 --> 00:06:00,280
So remove the try catch block completely.

83
00:06:00,430 --> 00:06:07,090
And at this point, you should only have these two lines of code where we are waiting to find all the

84
00:06:07,090 --> 00:06:07,660
tasks.

85
00:06:07,840 --> 00:06:11,200
And if we're successful, we send back those tasks.

86
00:06:11,560 --> 00:06:17,560
And as far as the async wrapper, we take our controller as a argument.

87
00:06:17,950 --> 00:06:25,780
And since we return the function, we have access to REC or as a act of that are coming from the Express

88
00:06:26,110 --> 00:06:29,020
and we set up to try catch block.

89
00:06:29,170 --> 00:06:37,050
And in here we have a wait because our controller is still async and we know that by default, async

90
00:06:37,060 --> 00:06:40,720
function will always, always return a promise.

91
00:06:41,050 --> 00:06:49,090
SERN here I just have a wait and I'm waiting for that promise to be settled, either resolved or rejected.

92
00:06:49,300 --> 00:06:55,240
And since my controller will still need to access arrack address and possibly next, since I get them

93
00:06:55,240 --> 00:07:01,080
from Express, I pass it down to my controller and then if there is an error.

94
00:07:01,090 --> 00:07:09,880
So in here, if we trigger some kind of error, whether in get all tasks or any of the controllers,

95
00:07:10,210 --> 00:07:17,710
then of course we'll catch it over here and we'll pass it to a next set of Middler, which we haven't

96
00:07:17,860 --> 00:07:18,680
set up yet.

97
00:07:19,000 --> 00:07:23,740
And now, of course, once we have everything in place now, we just want to apply these changes to

98
00:07:23,740 --> 00:07:24,660
all the rules.

99
00:07:24,970 --> 00:07:30,040
Now, I fully understand that some people are very negative about refactoring the code.

100
00:07:30,220 --> 00:07:36,040
And to some degree I sympathize with that because I also hold the opinion where it's not very useful

101
00:07:36,280 --> 00:07:39,280
if we're all the time refactoring the application.

102
00:07:39,790 --> 00:07:46,630
But it was important for me to show you the long way first, because that way you have a good understanding

103
00:07:46,870 --> 00:07:48,600
of how everything works.

104
00:07:48,970 --> 00:07:52,860
And now we just set up the middleware that actually handles that.

105
00:07:53,140 --> 00:07:55,330
So let's go back to the controllers.

106
00:07:55,570 --> 00:08:03,190
And slowly but surely, let's add this, a single wrapper to all the routes and the setup is going to

107
00:08:03,190 --> 00:08:06,910
be following for all of them, or we start with a single wrapper.

108
00:08:07,210 --> 00:08:12,580
So we set up our two parentheses here and we need to make sure that second parentheses, the closing

109
00:08:12,580 --> 00:08:15,340
one is a write off thirty curly brace.

110
00:08:15,490 --> 00:08:15,910
Correct.

111
00:08:16,240 --> 00:08:20,260
Then we want to remove the try block as well as the catch block.

112
00:08:20,560 --> 00:08:27,130
And again, we'll be just left with a weight that does not create if we're successful, we send back

113
00:08:27,130 --> 00:08:31,620
the task and fact that we want to do that for all our controllers.

114
00:08:32,049 --> 00:08:34,610
So let me go here with a single wrapper.

115
00:08:34,929 --> 00:08:37,840
Same deal for this guy here.

116
00:08:38,289 --> 00:08:45,190
And we have try and we remove the catch, but we are leaving this one.

117
00:08:45,670 --> 00:08:47,830
So we'll work with this one a little bit later.

118
00:08:48,160 --> 00:08:51,880
For the time being, we'll leave it as it is with this response.

119
00:08:52,210 --> 00:08:56,380
And once we set up our customer, there is going to be some changes here as well.

120
00:08:56,800 --> 00:08:58,120
Let's keep on scrolling.

121
00:08:58,150 --> 00:08:59,950
We're looking for the async wrapper.

122
00:09:00,640 --> 00:09:05,470
And again, we want to be closing one right after the curly brace.

123
00:09:05,740 --> 00:09:07,330
We still passing the function.

124
00:09:07,600 --> 00:09:13,390
Our move might try or might catch here and we'll save it.

125
00:09:13,390 --> 00:09:16,810
And the same goes for updating a task as well.

126
00:09:17,170 --> 00:09:21,850
So we go here with a single wrapper and we'll close it.

127
00:09:23,030 --> 00:09:25,790
Then we want to try.

128
00:09:26,590 --> 00:09:28,360
And we want to remove Kuch.

129
00:09:29,290 --> 00:09:37,050
And just to try it out, why don't we go to create a task and then I'm just going to go with creating

130
00:09:37,060 --> 00:09:39,380
task, creating task.

131
00:09:39,430 --> 00:09:40,360
Let's try it out.

132
00:09:40,750 --> 00:09:41,990
Everything still works.

133
00:09:42,010 --> 00:09:50,050
I can create my task, but if I'll try to add a task with an empty string value, I should get the error.

134
00:09:50,250 --> 00:09:51,030
And I do.

135
00:09:51,040 --> 00:09:53,620
But of course, this is not the error that we're sending.

136
00:09:53,890 --> 00:09:57,040
So that's something that we're going to work on in the upcoming videos.

