WEBVTT

00:03.820 --> 00:05.400
Hey, did everyone had a share?

00:05.410 --> 00:10.090
And in this video, we're going to talk again about the big promise in case you remember from one of

00:10.090 --> 00:11.020
the previous video.

00:11.050 --> 00:13.150
I told you that I'm worried about a couple of things.

00:13.150 --> 00:17.800
For example, if I go ahead and take a look onto this home controller right now, it is not connected

00:17.800 --> 00:18.550
to the database.

00:18.550 --> 00:22.930
So I'm not worried about whether the request is going to be real time or a sink of it.

00:22.930 --> 00:23.890
I have to use that.

00:23.890 --> 00:25.200
So that is one thing.

00:25.210 --> 00:28.600
The second thing I'm worried about whether the connection will actually happen or not.

00:28.600 --> 00:32.980
So I need to use a try catch block or in case I don't want to use any both of them.

00:32.980 --> 00:37.960
I need to go through and wrap everything around into a promise, a big promise.

00:37.960 --> 00:40.210
So that's exactly why I create a big promise.

00:40.210 --> 00:42.880
And I'll show you how you can use this big promise right up here.

00:42.880 --> 00:47.320
So let me let's go ahead and transform these basic home routes into these big promises.

00:47.320 --> 00:49.780
This is basically a middleware that we create.

00:49.780 --> 00:55.180
So let's go ahead and remember, whenever you create a middleware, usually like 99% of the time, you'll

00:55.180 --> 00:56.920
be using the next keyword there.

00:56.920 --> 00:58.420
So let's go ahead and use this one.

00:58.420 --> 01:05.860
So I'll be calling this one as simply Big Promise is now just like as I mentioned in the previous video,

01:05.860 --> 01:07.690
we'll be using exactly same code.

01:07.690 --> 01:10.030
So we're going to say, Hey, I want to just go ahead and export.

01:10.030 --> 01:12.550
This functions directly without even naming it.

01:12.640 --> 01:14.350
You can name it, no problem there.

01:14.350 --> 01:14.920
We have seen that.

01:14.920 --> 01:20.050
But this is how usually I do it and everybody else also that has seen they also do it exactly same.

01:20.050 --> 01:27.400
So what it does you take request response and the next obviously and you just pass it on.

01:27.400 --> 01:31.120
So I have taken a function and I have just directly handed over to this function.

01:31.120 --> 01:34.360
So I'm treating these functions as a first class citizen just like a variable.

01:34.360 --> 01:36.340
I took a variable, I pass it on as it is.

01:36.340 --> 01:37.930
So that's exactly what I'm doing.

01:37.930 --> 01:40.720
I took a function and I'm passing on exactly there.

01:40.720 --> 01:43.570
So how we are going to do it and how you are passing it up.

01:43.570 --> 01:48.820
I'm going further down the road and I'm directly saying that, hey, you will be handled by a promise

01:48.820 --> 01:49.510
itself.

01:49.510 --> 01:57.100
Now promise obviously can resolve or promise can go bad and we can go ahead and see the next catch.

01:57.250 --> 02:00.940
Now again, I would like to put up a comment for you that why we are actually doing it.

02:00.940 --> 02:11.140
So why we are doing it because I either I have to use the tri catch and we have to use a sink and think

02:11.140 --> 02:11.860
of it.

02:13.240 --> 02:18.670
And other option that we have is kind of a use promise everywhere.

02:18.670 --> 02:23.170
So that's exactly kind of a kind of a missing around the things here.

02:23.170 --> 02:28.030
So once we have this resolved, so in case the resolve is happening, that means the function is executing.

02:28.030 --> 02:34.510
So it is having a request response and the next and in case everything goes bad, then we just get the

02:34.510 --> 02:36.550
next as back again.

02:36.670 --> 02:42.040
I know this is a little bit too much because having a control over functional programming or having

02:42.040 --> 02:43.870
understanding might take a little bit.

02:43.870 --> 02:47.830
But I promise you, this is the only functional programming that I'll be using because otherwise this

02:47.830 --> 02:51.430
method becomes too long to write and it's not much useful.

02:51.640 --> 02:52.090
Okay.

02:52.510 --> 02:54.730
Once you have done this one, then how come?

02:54.730 --> 02:56.800
Actually, I go ahead and use this up here.

02:56.800 --> 02:59.380
The first thing is to bring this function up here.

02:59.380 --> 03:02.800
So what we're going to do is we are going to bring this function up here.

03:02.800 --> 03:05.140
Let's go ahead and do that at the very top here.

03:05.530 --> 03:08.800
So I'm going to call it as const, let's call it as big.

03:08.800 --> 03:11.320
I can call it anything because I didn't named it in that file.

03:11.320 --> 03:14.950
So I'm going to go ahead and call this one as big promise.

03:15.190 --> 03:23.440
And Big Promise is going to come up from require let's go one directory back of course encodes one directory

03:23.440 --> 03:28.150
back inside the middleware and inside the middleware we got this big promise.

03:28.210 --> 03:31.540
Okay, now what is going to happen once you have this big promise?

03:31.540 --> 03:33.250
Now it's really, really simple.

03:33.280 --> 03:36.100
All you got to do is now you can make anything a think of it.

03:36.100 --> 03:36.940
So I'm going to go ahead.

03:36.940 --> 03:42.460
Or even if you don't want to make it, you can just go ahead and cut this out entirely because remember

03:42.640 --> 03:47.650
our big promise except a function and what is the true definition of the function here?

03:47.650 --> 03:49.720
It starts request, response and all of that.

03:49.720 --> 03:53.890
So I can literally cut this out and I can say, Hey, big promise.

03:55.170 --> 03:55.770
There we go.

03:55.770 --> 03:58.340
And inside this big promise, I'm going to go ahead and pass it on.

03:58.350 --> 04:02.130
That's exactly that's exactly what I have to do now inside this.

04:02.130 --> 04:07.260
If I want to make it a sink and a weight, I can go ahead and say, hey, I want to use a sync and probably

04:07.260 --> 04:17.460
I'll use a weight wherever I'm declaring available const db and that is equals to await something.

04:17.760 --> 04:18.930
So something is happening again.

04:18.930 --> 04:22.710
This is just for making sure you understand what is happening, how is it happening?

04:22.710 --> 04:23.400
And that's it.

04:23.400 --> 04:24.780
That's basically all we do.

04:24.810 --> 04:29.490
We just wanted to make sure that the method that we are writing is actually in the collaboration with

04:29.490 --> 04:30.300
the promises.

04:30.300 --> 04:32.370
That's why we are wrapping everything around.

04:32.370 --> 04:37.410
So I hope this actually gives you a little bit of more clarity now that this big promise actually takes

04:37.410 --> 04:40.290
a function and try to execute that in the promise way.

04:40.530 --> 04:46.020
And remember, this is a function itself, so it receives a function and try to execute it up here.

04:46.020 --> 04:49.350
That's exactly what we are doing and we will be doing this throughout the course.

04:49.350 --> 04:53.880
Take this big promise and wrap around your regular of these methods around this big promise.

04:53.880 --> 04:55.800
And that is why Big Promise is there.

04:55.800 --> 05:00.810
In case you don't want to use Big Promise, then what you have to do, you have to use the tri catch

05:00.810 --> 05:02.850
and you have to use the async and await.

05:02.850 --> 05:06.630
So whenever you're using a single event, make sure you are using tricks.

05:06.760 --> 05:10.290
So let me give you an example of that as well in case you are getting confused here.

05:10.410 --> 05:12.150
So you have to wrap up everything.

05:13.450 --> 05:14.980
With they try and catch.

05:14.980 --> 05:23.200
So this whole thing needs to go inside up here and then handle the catcher response.

05:23.200 --> 05:24.850
Maybe you want to just log this one here.

05:24.850 --> 05:26.590
So let's go ahead and log the error.

05:26.740 --> 05:34.060
And once this is all done, then you can go ahead and make your function as a sync and same line.

05:34.060 --> 05:35.080
I can bottle that.

05:35.080 --> 05:36.010
So there we go.

05:36.280 --> 05:38.170
Okay, so which one is correct?

05:38.170 --> 05:39.140
Which one is accurate?

05:39.160 --> 05:40.270
Feel free to use anything.

05:40.270 --> 05:43.990
And there can be a third version which we discussed that, hey, just use the promises whenever you

05:43.990 --> 05:44.650
need that.

05:44.650 --> 05:48.790
And in this way I can use just my regular codes and all of that, and I can go with that.

05:48.790 --> 05:50.590
So I will be going with this method.

05:50.920 --> 05:55.360
Feel free to go ahead and use the try catch method with the thinking of it and a whole bunch of things.

05:55.750 --> 06:00.880
I know this is the most complicated part of this entire course in case you have don't have much experience

06:00.880 --> 06:05.980
with the this kind of this level of functional programming again want to use it use it.

06:05.980 --> 06:09.610
Otherwise, just make sure that you have this big promise and we'll be wrapping around everything with

06:09.610 --> 06:10.240
the vague promise.

06:10.240 --> 06:11.290
That's all I'm saying.

06:11.290 --> 06:13.330
Let's go ahead and catch up in the next video.
