Hey, did everyone had a share?
And in this video, we're going to talk again about the big promise in case you remember from one of
the previous video.
I told you that I'm worried about a couple of things.
For example, if I go ahead and take a look onto this home controller right now, it is not connected
to the database.
So I'm not worried about whether the request is going to be real time or a sink of it.
I have to use that.
So that is one thing.
The second thing I'm worried about whether the connection will actually happen or not.
So I need to use a try catch block or in case I don't want to use any both of them.
I need to go through and wrap everything around into a promise, a big promise.
So that's exactly why I create a big promise.
And I'll show you how you can use this big promise right up here.
So let me let's go ahead and transform these basic home routes into these big promises.
This is basically a middleware that we create.
So let's go ahead and remember, whenever you create a middleware, usually like 99% of the time, you'll
be using the next keyword there.
So let's go ahead and use this one.
So I'll be calling this one as simply Big Promise is now just like as I mentioned in the previous video,
we'll be using exactly same code.
So we're going to say, Hey, I want to just go ahead and export.
This functions directly without even naming it.
You can name it, no problem there.
We have seen that.
But this is how usually I do it and everybody else also that has seen they also do it exactly same.
So what it does you take request response and the next obviously and you just pass it on.
So I have taken a function and I have just directly handed over to this function.
So I'm treating these functions as a first class citizen just like a variable.
I took a variable, I pass it on as it is.
So that's exactly what I'm doing.
I took a function and I'm passing on exactly there.
So how we are going to do it and how you are passing it up.
I'm going further down the road and I'm directly saying that, hey, you will be handled by a promise
itself.
Now promise obviously can resolve or promise can go bad and we can go ahead and see the next catch.
Now again, I would like to put up a comment for you that why we are actually doing it.
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
of it.
And other option that we have is kind of a use promise everywhere.
So that's exactly kind of a kind of a missing around the things here.
So once we have this resolved, so in case the resolve is happening, that means the function is executing.
So it is having a request response and the next and in case everything goes bad, then we just get the
next as back again.
I know this is a little bit too much because having a control over functional programming or having
understanding might take a little bit.
But I promise you, this is the only functional programming that I'll be using because otherwise this
method becomes too long to write and it's not much useful.
Okay.
Once you have done this one, then how come?
Actually, I go ahead and use this up here.
The first thing is to bring this function up here.
So what we're going to do is we are going to bring this function up here.
Let's go ahead and do that at the very top here.
So I'm going to call it as const, let's call it as big.
I can call it anything because I didn't named it in that file.
So I'm going to go ahead and call this one as big promise.
And Big Promise is going to come up from require let's go one directory back of course encodes one directory
back inside the middleware and inside the middleware we got this big promise.
Okay, now what is going to happen once you have this big promise?
Now it's really, really simple.
All you got to do is now you can make anything a think of it.
So I'm going to go ahead.
Or even if you don't want to make it, you can just go ahead and cut this out entirely because remember
our big promise except a function and what is the true definition of the function here?
It starts request, response and all of that.
So I can literally cut this out and I can say, Hey, big promise.
There we go.
And inside this big promise, I'm going to go ahead and pass it on.
That's exactly that's exactly what I have to do now inside this.
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
I'll use a weight wherever I'm declaring available const db and that is equals to await something.
So something is happening again.
This is just for making sure you understand what is happening, how is it happening?
And that's it.
That's basically all we do.
We just wanted to make sure that the method that we are writing is actually in the collaboration with
the promises.
That's why we are wrapping everything around.
So I hope this actually gives you a little bit of more clarity now that this big promise actually takes
a function and try to execute that in the promise way.
And remember, this is a function itself, so it receives a function and try to execute it up here.
That's exactly what we are doing and we will be doing this throughout the course.
Take this big promise and wrap around your regular of these methods around this big promise.
And that is why Big Promise is there.
In case you don't want to use Big Promise, then what you have to do, you have to use the tri catch
and you have to use the async and await.
So whenever you're using a single event, make sure you are using tricks.
So let me give you an example of that as well in case you are getting confused here.
So you have to wrap up everything.
With they try and catch.
So this whole thing needs to go inside up here and then handle the catcher response.
Maybe you want to just log this one here.
So let's go ahead and log the error.
And once this is all done, then you can go ahead and make your function as a sync and same line.
I can bottle that.
So there we go.
Okay, so which one is correct?
Which one is accurate?
Feel free to use anything.
And there can be a third version which we discussed that, hey, just use the promises whenever you
need that.
And in this way I can use just my regular codes and all of that, and I can go with that.
So I will be going with this method.
Feel free to go ahead and use the try catch method with the thinking of it and a whole bunch of things.
I know this is the most complicated part of this entire course in case you have don't have much experience
with the this kind of this level of functional programming again want to use it use it.
Otherwise, just make sure that you have this big promise and we'll be wrapping around everything with
the vague promise.
That's all I'm saying.
Let's go ahead and catch up in the next video.