Welcome back.
In this video, let's see how we can create a function that retains a promise.
And this is what we are going to use in rare use cases.
So let's get started.
Perfect.
So coming back to our code, we are now going to refactor this using a function perfect.
So the logic is the same.
We are going to create a function to fetch a user from database.
I mean, we are trying to mimic.
All right, so let's get started.
So let me comment.
This one's out here for the meantime as that.
And now let's come down here and says.
Function.
Retain.
Returns a promise or function in returning a promise.
I think there's going to be good function in returning a promise I'll keep.
I'm going to use the same logic about fetching user.
So for function creation, we all know it.
So let's go ahead and create a function.
So going to be get user.
Okay.
Perfect.
So here we go.
So here because we are going to make this a promise because we don't know when we are going to get back
the user.
We need to make this promise inside this particular function.
So here I'll make use of neon.
Remember how we create a promise and as a function call.
And for this function I pass in one agreement with my callback and I'm going to use arrow function here.
So inside my callback, I have access to my resolve and then reject.
Perfect.
So here is our our logic goes here.
Let's say logic goes here.
So the same logic we use here let's is first to true and user to this object so I can bring this object
inside here.
So let's have the logic as before.
Let's user is equal to my object.
Going to be name which is John.
And then the city of this person is from Accra.
Perfect.
So now let's have a condition that determines that our function is being called, which is is fetched,
is equal to true.
Perfect.
Now let's save it.
So let's make use of these methods here.
Let's say resolve.
And then reject.
Now for this one, I believe you know how to use it.
So now for resolve.
Let's bring our resolve here and now for rejects.
Let's also bring the reject here.
So now let's conditionally I mean, retain one of these ones.
Do we get it?
Yes, we get it.
So here we can say that if it's fetched, if is fetched, then we are going to call resolve.
So for reject this function will be called.
So here you can bring in errors or we can ignore it.
So let's make it simple by bringing it inside our statement.
All right.
So now you want to see the actual user, so we pass in the user to my result for this one.
There is no difference here the same thing as we did before.
But for this one we are just wrapping it inside a function.
That is why we see here.
So here going to be the same thing.
The difference here is about the outer function.
Okay, so remember we talk about function as a first class citizen.
Now here we go.
We see that all what we need from this function is to retain what is coming back from this promise.
Remember, a function can return and a function that's what you call function at first class citizen.
Perfect.
So here what comes back for this from this one is a promise whether for float or reject.
So here we can assign the promise to a variable called promise and equal to that meaning.
We need to return this promise, which is a function or result coming back from this one.
So what comes back from this one is a value by instance, an object.
So here, after this one, it means that after this one, we need to also call or retain the promise
from this function.
So here I will come down here and I will say return promise.
So now we have a perfect syntax.
So inside of this way we can even shorten it.
That is, we assign a value to the, to this I mean promise here.
And the next is we call down here so we can call it immediately by making some of this.
So here we can remove this one and big and bring retain.
So here we go.
So now I can remove this one, which is equally as before.
So the choice is yours.
So if I call this function, I'm going to give me a promise.
So check it out.
So if I call this function, let's try to console.log this function and see.
So const, let's say result.
Equal to this.
So if I console.log the results, I'll check it out.
And we see that as before.
Now we have something happening.
As before we have a field and we have an object with this.
So it's time to consume the promise.
Which now brings us to this particular point.
As I make mention that when returning a promise from a function, the function return a promise, that's
what we see here.
You see that the function is returning a promise.
So the next point says that well and the value of the promise will be retained when the promise is resolved.
Meaning we need to resolve this promise.
And now you know how we resolve a promise, right?
So now we make this off the get user.
All right, get Utzon.
And then as a function call assist, then dot, then remember.
So here we pass in our callback function and then function as that.
And here we have the data coming back.
Or response.
Let's say response.
And then.
Now.
Now I can console.log the response or data if I save it.
Now we have it as before.
That is it.
So what about if something goes wrong?
Let's see.
Now let's change this one to force to render our application to fail.
So now we see that the same error on code error because we are not handling the error.
So here the catch.
Now I'm going to catch the error passing in my callback error here and here you go.
Console.log, console.log, the error.
And now let's see now undefined.
Because for this reject you're not passing anything.
Let's see that error.
Fetching.
User.
Okay.
Yes, save it.
And now indeed we have error fetching user.
So don't worry.
If you get back to how to make HTTP call with JavaScript, all these things will make sense than just
us being creative and mean.
Try to mimic the way we are making HTTP calls here.
So basically understand how fancy sorry a promise can be created and how we can resolve and reject.
That is the most important thing about this section.
As you move on, we are going to practice more in more rare use cases.
So in the next video, we have one last topic to talk about.
That is how we can consume multiple promises.
All right.
Let's get into that, the next video.