Hello.
So we talked about the use of Fact Hook in the previous video, and now we are going to talk about using
functions in JavaScript or dressing functions.
But before getting to the async functions, you need to know what are promises.
Because you think functions that are done promises and before going to Issing functions, you need to
know what our promises and so if we don't know promises, you will not understand anything functional.
So that's why I'm going to go to what almost promises first.
No word is a promise or water promises, and the are left on the stand with the help of an article on
the official website of Jousted, which is the.
JavaScript thought this is a really, really important website if you want to learn JavaScript or react
because this has the documentation of everything almost on the script and on very, very simple and
basic language and from like a beginner to advance, all of it is covered at this website.
OK, so what are promises?
So here's a little story here, so I'll not bore you with while reading all of the stories.
I'll just give you the brief introduction about promises the promises are like in this there's a written
a story so I can tell that story and how it is written that.
Imagine you are a singer and your fans are just always messaging.
You always whenever you go out, they are troubling you and they're asking you when your next song will
come out, OK?
And you as a singer are tired of listening to that and you are just fed up like all of the audience
always asking about a new song or the new album.
And so what you do here is that you get too frustrated to know what you do is that you take you give
and link to your you give a link to your fan when building a form in which then they need to put very
melodies, OK?
You need to give the link of online form.
OK, so as soon as your audience received this form, so there they will have an option to fill out
a form and in that they need to fill that similarities the ones different melodies.
You will give a message to them that, OK, I feel your email it easier.
And as soon as my song or album will come up, you will get an email notification that the song or email
is up.
Not as simple as that.
So now what?
The singer or what you are doing is that you are making a promise to your audience, OK, you are making
a promise to that to your audience that, OK, I will be giving you a notification once my song or album
comes out, OK?
So this is the promise.
So this is how promise will be working in JavaScript.
Now the promise, there are two cases for the promise the promise can be resolved.
That means it can be fulfilled or it can all be fulfilled.
It can all be resolved so that it can be rejected.
Also, for example, if you upload the song and your album, then you know and notify your users or
your audience that, OK, I uploaded the song.
So in this case, the promise is resolved and it's returning resolve.
But what if you don't?
You are not able to deliver the song your studio gets blown.
All all of this is written OK just to do get born and you are not able to deliver the song.
So in that case, it's not a reject because it's not resolved, OK?
So that is what it said to me.
So this is a real life analogy of things we often have in programming.
So there's a programming code, so that does something in big time.
For instance, some code that lowered the data over a network that's a single.
So this takes time.
OK?
The producing code takes time.
Credits include Is the single.
The single will take some time to produce the song.
OK, so next we have a consuming code.
Now consuming code will be taking producing code as an input.
OK, keep this in mind.
This is very important to consuming code.
The result of the producing code once it's ready.
So once the song is ready, the consuming code of the consumer will need the do sync would output from
the producing code.
OK, so these are the fans.
And next, we talk about promises like water.
The promise to promise is just whatever the output of this food promises, especially JavaScript function
that links to the producing code.
I told you already and the commitment put together in terms of analogy, this is a subscription list.
This is a subscription list like we got the list of the users right out of which we took the image ladies,
the producing cortex or whatever time it needs to grow to the promised result and the promise mix that
isn't available to all subscribed when it is ready, so it will return the promise.
All right.
Now let's take a look at the basic syntax of this promise.
So promise is available.
It will be initialized like this new promise, and it will expect to function inside the function.
It will expect to arguments.
These are predefined to these arguments were to be resolved and reject.
To resolve is when the code is resolved and the promise is resolved.
So resolved will return in that case and reject will be returned.
If that promise is not fulfilled and the promise is not successful, so reject will be returning in
error.
OK, now this is the executor or the police and courts are here.
All the execution of the code will be done, the function will be called here and all execution will
be done here.
And after all, they will see that if the return in the desired value, then it's resolved and it's
rejected.
OK, so this all.
I'm not boring with all that.
Let me just tell you about this diagram, right?
Well, let me explain.
So new promise.
This is a new promise via email or the executor has made a new promise.
So before beginning the execution, the state of the promises pending?
All right.
And the result is undefined, obviously, before the execution, how can we tell that?
What is the result and where does the state state?
We can tell there the spending, but we can't predict the result.
OK, so the result is undefined before the execution.
All right.
And once it's executed and it returns, get resolved like the promised results.
So that's a green signal.
So once the promises are resolved, the state will change to fulfill.
Once the promises are ordered, the state would try to fulfill.
And the result will be the value which is written by the promise, by the function.
All right.
Now let's take a look at the next phase of it.
If the promise gets rejected and the promises not resolved, are the promises not fulfilled.
That means we receive an error.
OK, so in that case, the state would be rejected.
It would be rejected, and the result would be an error because we can't get our value.
If the promises reject, we will just get an error and the state will be rejected.
So that's all about the promises.
There are a lot of things.
For example, here the state is spending the desert doesn't define, but when it's the result, the
state does fulfill and the result is done.
Same as that.
Same as this.
So this is what this page will tell all about.
But there are a lot of more things also to the advanced, you can take a look and read this page.
This is really, really informative.
So you can learn about this at the bottom.
So that's all about the promises.
Now, once we know the promises, we will be learning about Ithinkhe and we know what you think to,
we will go here.
Cookies allow.
Now let me tell you about the async and await.
And for that, we need to go to the promises.
And then we'll go to asynchronously.
OK.
So the this is the documentation for that, you're thinking of it.
So let me just tell you what isn't.
So the dispersers index to work with promises in more comfortable fashion, async and await, you'll
see promise of work with promises.
So this battle promises only, but in a better way.
So it's surprisingly easy.
Just take a look at this line.
It's surprisingly easy.
That means this is the easy form of the promises.
Going to stand and use.
OK, and that we have isn't functioning, this is the basics and backs of this in function.
Let's start with the using keyword.
It is played before any function like this.
So it's since this is a normal function which we write in a script, but it will have this key word
before this name of the function.
Well, before the dudes function work, there will be a key word which is async, but that will make
this function is.
Now what does async function do?
Is in function is like returns up from its async function, will return a promise and we already talked
about promises.
All right.
So here we have a function which will return one and then we are alerting like alerting means like taking
what is done.
Well, we are alerting.
What is this function?
OK.
I have got an alert.
So this means alert.
This alert the return value of this function.
So we'll get the answer one, right?
And this is the promise resolved.
So that is one.
Now this is the promise, which is a.
So this is same as this because the the promise returns like that is our promise returns the result.
The result is one, OK?
But next, we will take a look at a way to know what is of it, because await and async used to show
that they are not like used separately.
The kind async await will be used inside they think only works only with async functions, either it's
already written in.
The values of it promises stood now, what will this do is that, for example, this is a async function
and this all code is written here inside this function.
The latter problem is that they are defining a promise sword as all this and that now light resolve
to the call to await promise.
Now what is this?
Are we do?
So this means this execution will go on here and this is executing.
Then after that, we'll come to this.
Now avetE will tell those group to wait for the execution of this.
This this entire thing, like whatever the function name we promised, will be whatever so it will.
This keyword will spell JavaScript to, Hey, wait, this function is executing.
So till then, you can do another thing, another one, but don't move further because this function
has been executed already.
So after the function is executed, like after we get the promise as resolved, so then the JavaScript
moves forward.
Yeah, you can see the key word of it makes JavaScript.
Wait until that promise settles and returns it.
It's the result.
So it's written clearly here.
So this is the basic thing and.
You can't use of it in regular functions, you need to use of it in the using function only only.
OK, so I wouldn't want to work on the top level code, it needs to be inside the async.
It will not work in the top low level code.
OK, so there are a lot of things here and inside the async and a weird function that I try and get
our best try and go.
So let's see if we can that we can catch the error using try and go the same way we use regular true
supply will.
But I will run this code, and if this code like returns are properly resolved, the value so then will
go get it.
Otherwise, if we get an error, it'll catch the error and we'll pass that error inside the sketch function
and then we can alert that error.
We can console log data.
So this was all about the async and from async functions.
I'll wait and try and catch and promises so.