1
00:00:00,780 --> 00:00:01,210
Hello.

2
00:00:01,290 --> 00:00:08,250
So we talked about the use of Fact Hook in the previous video, and now we are going to talk about using

3
00:00:08,250 --> 00:00:11,340
functions in JavaScript or dressing functions.

4
00:00:11,370 --> 00:00:17,670
But before getting to the async functions, you need to know what are promises.

5
00:00:18,720 --> 00:00:24,840
Because you think functions that are done promises and before going to Issing functions, you need to

6
00:00:24,840 --> 00:00:31,050
know what our promises and so if we don't know promises, you will not understand anything functional.

7
00:00:31,050 --> 00:00:33,990
So that's why I'm going to go to what almost promises first.

8
00:00:34,970 --> 00:00:43,070
No word is a promise or water promises, and the are left on the stand with the help of an article on

9
00:00:43,070 --> 00:00:46,280
the official website of Jousted, which is the.

10
00:00:47,230 --> 00:00:54,160
JavaScript thought this is a really, really important website if you want to learn JavaScript or react

11
00:00:54,850 --> 00:01:00,850
because this has the documentation of everything almost on the script and on very, very simple and

12
00:01:00,850 --> 00:01:08,320
basic language and from like a beginner to advance, all of it is covered at this website.

13
00:01:09,070 --> 00:01:11,560
OK, so what are promises?

14
00:01:12,220 --> 00:01:18,880
So here's a little story here, so I'll not bore you with while reading all of the stories.

15
00:01:18,910 --> 00:01:26,940
I'll just give you the brief introduction about promises the promises are like in this there's a written

16
00:01:26,950 --> 00:01:30,850
a story so I can tell that story and how it is written that.

17
00:01:31,000 --> 00:01:35,020
Imagine you are a singer and your fans are just always messaging.

18
00:01:35,020 --> 00:01:40,120
You always whenever you go out, they are troubling you and they're asking you when your next song will

19
00:01:40,120 --> 00:01:41,170
come out, OK?

20
00:01:41,200 --> 00:01:48,760
And you as a singer are tired of listening to that and you are just fed up like all of the audience

21
00:01:48,910 --> 00:01:51,310
always asking about a new song or the new album.

22
00:01:51,320 --> 00:02:01,090
And so what you do here is that you get too frustrated to know what you do is that you take you give

23
00:02:01,090 --> 00:02:08,140
and link to your you give a link to your fan when building a form in which then they need to put very

24
00:02:08,140 --> 00:02:09,020
melodies, OK?

25
00:02:09,280 --> 00:02:11,200
You need to give the link of online form.

26
00:02:11,410 --> 00:02:18,040
OK, so as soon as your audience received this form, so there they will have an option to fill out

27
00:02:18,040 --> 00:02:22,720
a form and in that they need to fill that similarities the ones different melodies.

28
00:02:22,720 --> 00:02:26,710
You will give a message to them that, OK, I feel your email it easier.

29
00:02:26,950 --> 00:02:33,400
And as soon as my song or album will come up, you will get an email notification that the song or email

30
00:02:33,400 --> 00:02:33,780
is up.

31
00:02:33,790 --> 00:02:35,590
Not as simple as that.

32
00:02:35,800 --> 00:02:36,770
So now what?

33
00:02:36,780 --> 00:02:42,970
The singer or what you are doing is that you are making a promise to your audience, OK, you are making

34
00:02:42,970 --> 00:02:49,390
a promise to that to your audience that, OK, I will be giving you a notification once my song or album

35
00:02:49,390 --> 00:02:50,260
comes out, OK?

36
00:02:50,680 --> 00:02:52,150
So this is the promise.

37
00:02:53,140 --> 00:02:56,140
So this is how promise will be working in JavaScript.

38
00:02:56,380 --> 00:03:03,820
Now the promise, there are two cases for the promise the promise can be resolved.

39
00:03:04,000 --> 00:03:08,070
That means it can be fulfilled or it can all be fulfilled.

40
00:03:08,080 --> 00:03:12,340
It can all be resolved so that it can be rejected.

41
00:03:12,340 --> 00:03:18,670
Also, for example, if you upload the song and your album, then you know and notify your users or

42
00:03:18,670 --> 00:03:22,120
your audience that, OK, I uploaded the song.

43
00:03:22,600 --> 00:03:27,990
So in this case, the promise is resolved and it's returning resolve.

44
00:03:28,330 --> 00:03:29,920
But what if you don't?

45
00:03:30,280 --> 00:03:33,800
You are not able to deliver the song your studio gets blown.

46
00:03:33,820 --> 00:03:39,370
All all of this is written OK just to do get born and you are not able to deliver the song.

47
00:03:39,610 --> 00:03:43,600
So in that case, it's not a reject because it's not resolved, OK?

48
00:03:44,560 --> 00:03:46,420
So that is what it said to me.

49
00:03:46,510 --> 00:03:50,770
So this is a real life analogy of things we often have in programming.

50
00:03:51,040 --> 00:03:56,290
So there's a programming code, so that does something in big time.

51
00:03:56,500 --> 00:03:59,920
For instance, some code that lowered the data over a network that's a single.

52
00:04:00,220 --> 00:04:01,460
So this takes time.

53
00:04:01,480 --> 00:04:01,870
OK?

54
00:04:02,800 --> 00:04:04,360
The producing code takes time.

55
00:04:04,360 --> 00:04:05,700
Credits include Is the single.

56
00:04:05,710 --> 00:04:08,470
The single will take some time to produce the song.

57
00:04:08,500 --> 00:04:11,200
OK, so next we have a consuming code.

58
00:04:11,380 --> 00:04:16,210
Now consuming code will be taking producing code as an input.

59
00:04:16,660 --> 00:04:17,860
OK, keep this in mind.

60
00:04:17,860 --> 00:04:20,460
This is very important to consuming code.

61
00:04:20,890 --> 00:04:23,410
The result of the producing code once it's ready.

62
00:04:23,710 --> 00:04:30,490
So once the song is ready, the consuming code of the consumer will need the do sync would output from

63
00:04:30,490 --> 00:04:31,420
the producing code.

64
00:04:31,450 --> 00:04:33,430
OK, so these are the fans.

65
00:04:33,700 --> 00:04:36,130
And next, we talk about promises like water.

66
00:04:36,170 --> 00:04:43,450
The promise to promise is just whatever the output of this food promises, especially JavaScript function

67
00:04:43,450 --> 00:04:45,230
that links to the producing code.

68
00:04:45,250 --> 00:04:51,580
I told you already and the commitment put together in terms of analogy, this is a subscription list.

69
00:04:51,850 --> 00:04:57,070
This is a subscription list like we got the list of the users right out of which we took the image ladies,

70
00:04:57,490 --> 00:05:02,680
the producing cortex or whatever time it needs to grow to the promised result and the promise mix that

71
00:05:02,680 --> 00:05:06,590
isn't available to all subscribed when it is ready, so it will return the promise.

72
00:05:06,620 --> 00:05:06,970
All right.

73
00:05:07,270 --> 00:05:10,750
Now let's take a look at the basic syntax of this promise.

74
00:05:11,140 --> 00:05:12,550
So promise is available.

75
00:05:12,550 --> 00:05:19,350
It will be initialized like this new promise, and it will expect to function inside the function.

76
00:05:19,360 --> 00:05:21,730
It will expect to arguments.

77
00:05:22,240 --> 00:05:26,410
These are predefined to these arguments were to be resolved and reject.

78
00:05:26,860 --> 00:05:31,720
To resolve is when the code is resolved and the promise is resolved.

79
00:05:31,960 --> 00:05:35,470
So resolved will return in that case and reject will be returned.

80
00:05:35,470 --> 00:05:42,040
If that promise is not fulfilled and the promise is not successful, so reject will be returning in

81
00:05:42,040 --> 00:05:42,430
error.

82
00:05:42,460 --> 00:05:47,500
OK, now this is the executor or the police and courts are here.

83
00:05:47,500 --> 00:05:52,630
All the execution of the code will be done, the function will be called here and all execution will

84
00:05:52,630 --> 00:05:53,350
be done here.

85
00:05:53,560 --> 00:06:00,510
And after all, they will see that if the return in the desired value, then it's resolved and it's

86
00:06:00,730 --> 00:06:01,420
rejected.

87
00:06:01,690 --> 00:06:02,680
OK, so this all.

88
00:06:02,680 --> 00:06:04,180
I'm not boring with all that.

89
00:06:04,180 --> 00:06:06,550
Let me just tell you about this diagram, right?

90
00:06:07,240 --> 00:06:08,200
Well, let me explain.

91
00:06:08,860 --> 00:06:10,000
So new promise.

92
00:06:10,300 --> 00:06:14,620
This is a new promise via email or the executor has made a new promise.

93
00:06:15,100 --> 00:06:21,370
So before beginning the execution, the state of the promises pending?

94
00:06:21,550 --> 00:06:21,920
All right.

95
00:06:22,210 --> 00:06:26,450
And the result is undefined, obviously, before the execution, how can we tell that?

96
00:06:26,650 --> 00:06:29,220
What is the result and where does the state state?

97
00:06:29,230 --> 00:06:32,770
We can tell there the spending, but we can't predict the result.

98
00:06:32,770 --> 00:06:35,320
OK, so the result is undefined before the execution.

99
00:06:35,650 --> 00:06:36,060
All right.

100
00:06:36,220 --> 00:06:42,220
And once it's executed and it returns, get resolved like the promised results.

101
00:06:42,580 --> 00:06:43,960
So that's a green signal.

102
00:06:44,260 --> 00:06:48,130
So once the promises are resolved, the state will change to fulfill.

103
00:06:49,070 --> 00:06:51,800
Once the promises are ordered, the state would try to fulfill.

104
00:06:52,070 --> 00:06:58,370
And the result will be the value which is written by the promise, by the function.

105
00:06:58,640 --> 00:06:59,000
All right.

106
00:06:59,180 --> 00:07:02,570
Now let's take a look at the next phase of it.

107
00:07:02,930 --> 00:07:09,230
If the promise gets rejected and the promises not resolved, are the promises not fulfilled.

108
00:07:09,440 --> 00:07:11,210
That means we receive an error.

109
00:07:11,600 --> 00:07:15,410
OK, so in that case, the state would be rejected.

110
00:07:15,650 --> 00:07:21,080
It would be rejected, and the result would be an error because we can't get our value.

111
00:07:21,080 --> 00:07:26,570
If the promises reject, we will just get an error and the state will be rejected.

112
00:07:26,860 --> 00:07:28,880
So that's all about the promises.

113
00:07:29,150 --> 00:07:30,560
There are a lot of things.

114
00:07:31,070 --> 00:07:31,880
There are a lot of things.

115
00:07:31,880 --> 00:07:36,500
For example, here the state is spending the desert doesn't define, but when it's the result, the

116
00:07:36,500 --> 00:07:38,420
state does fulfill and the result is done.

117
00:07:39,170 --> 00:07:39,890
Same as that.

118
00:07:39,890 --> 00:07:40,880
Same as this.

119
00:07:42,290 --> 00:07:46,370
So this is what this page will tell all about.

120
00:07:46,610 --> 00:07:52,130
But there are a lot of more things also to the advanced, you can take a look and read this page.

121
00:07:52,430 --> 00:07:54,020
This is really, really informative.

122
00:07:54,260 --> 00:07:56,840
So you can learn about this at the bottom.

123
00:07:57,230 --> 00:07:59,750
So that's all about the promises.

124
00:08:00,060 --> 00:08:07,980
Now, once we know the promises, we will be learning about Ithinkhe and we know what you think to,

125
00:08:08,050 --> 00:08:09,710
we will go here.

126
00:08:11,510 --> 00:08:12,290
Cookies allow.

127
00:08:12,320 --> 00:08:15,980
Now let me tell you about the async and await.

128
00:08:16,550 --> 00:08:20,510
And for that, we need to go to the promises.

129
00:08:21,590 --> 00:08:24,450
And then we'll go to asynchronously.

130
00:08:25,250 --> 00:08:25,590
OK.

131
00:08:26,480 --> 00:08:29,860
So the this is the documentation for that, you're thinking of it.

132
00:08:30,450 --> 00:08:32,900
So let me just tell you what isn't.

133
00:08:33,320 --> 00:08:39,740
So the dispersers index to work with promises in more comfortable fashion, async and await, you'll

134
00:08:39,740 --> 00:08:41,910
see promise of work with promises.

135
00:08:41,910 --> 00:08:44,690
So this battle promises only, but in a better way.

136
00:08:45,110 --> 00:08:46,810
So it's surprisingly easy.

137
00:08:46,820 --> 00:08:48,200
Just take a look at this line.

138
00:08:48,200 --> 00:08:49,550
It's surprisingly easy.

139
00:08:49,550 --> 00:08:53,090
That means this is the easy form of the promises.

140
00:08:54,150 --> 00:08:55,140
Going to stand and use.

141
00:08:57,380 --> 00:09:02,090
OK, and that we have isn't functioning, this is the basics and backs of this in function.

142
00:09:02,480 --> 00:09:03,910
Let's start with the using keyword.

143
00:09:03,920 --> 00:09:06,140
It is played before any function like this.

144
00:09:06,140 --> 00:09:11,960
So it's since this is a normal function which we write in a script, but it will have this key word

145
00:09:11,960 --> 00:09:13,760
before this name of the function.

146
00:09:15,430 --> 00:09:21,610
Well, before the dudes function work, there will be a key word which is async, but that will make

147
00:09:21,610 --> 00:09:22,640
this function is.

148
00:09:23,200 --> 00:09:26,380
Now what does async function do?

149
00:09:26,650 --> 00:09:36,520
Is in function is like returns up from its async function, will return a promise and we already talked

150
00:09:36,520 --> 00:09:37,390
about promises.

151
00:09:37,750 --> 00:09:38,200
All right.

152
00:09:38,470 --> 00:09:45,160
So here we have a function which will return one and then we are alerting like alerting means like taking

153
00:09:45,160 --> 00:09:46,510
what is done.

154
00:09:46,510 --> 00:09:47,980
Well, we are alerting.

155
00:09:49,180 --> 00:09:50,530
What is this function?

156
00:09:50,860 --> 00:09:51,250
OK.

157
00:09:51,340 --> 00:09:52,600
I have got an alert.

158
00:09:52,870 --> 00:09:53,740
So this means alert.

159
00:09:53,740 --> 00:09:56,080
This alert the return value of this function.

160
00:09:56,080 --> 00:09:57,940
So we'll get the answer one, right?

161
00:09:58,660 --> 00:10:01,140
And this is the promise resolved.

162
00:10:01,240 --> 00:10:01,990
So that is one.

163
00:10:02,450 --> 00:10:05,110
Now this is the promise, which is a.

164
00:10:05,610 --> 00:10:12,100
So this is same as this because the the promise returns like that is our promise returns the result.

165
00:10:12,400 --> 00:10:13,540
The result is one, OK?

166
00:10:13,900 --> 00:10:19,510
But next, we will take a look at a way to know what is of it, because await and async used to show

167
00:10:19,520 --> 00:10:22,630
that they are not like used separately.

168
00:10:22,630 --> 00:10:28,540
The kind async await will be used inside they think only works only with async functions, either it's

169
00:10:28,540 --> 00:10:29,410
already written in.

170
00:10:30,760 --> 00:10:38,580
The values of it promises stood now, what will this do is that, for example, this is a async function

171
00:10:38,580 --> 00:10:41,580
and this all code is written here inside this function.

172
00:10:42,040 --> 00:10:47,790
The latter problem is that they are defining a promise sword as all this and that now light resolve

173
00:10:47,790 --> 00:10:49,710
to the call to await promise.

174
00:10:50,190 --> 00:10:51,690
Now what is this?

175
00:10:51,690 --> 00:10:52,620
Are we do?

176
00:10:52,860 --> 00:10:57,930
So this means this execution will go on here and this is executing.

177
00:10:57,960 --> 00:11:00,240
Then after that, we'll come to this.

178
00:11:00,540 --> 00:11:06,150
Now avetE will tell those group to wait for the execution of this.

179
00:11:06,150 --> 00:11:12,070
This this entire thing, like whatever the function name we promised, will be whatever so it will.

180
00:11:12,090 --> 00:11:16,530
This keyword will spell JavaScript to, Hey, wait, this function is executing.

181
00:11:16,770 --> 00:11:23,250
So till then, you can do another thing, another one, but don't move further because this function

182
00:11:23,250 --> 00:11:24,460
has been executed already.

183
00:11:25,470 --> 00:11:34,080
So after the function is executed, like after we get the promise as resolved, so then the JavaScript

184
00:11:34,080 --> 00:11:34,980
moves forward.

185
00:11:35,070 --> 00:11:38,480
Yeah, you can see the key word of it makes JavaScript.

186
00:11:38,490 --> 00:11:41,670
Wait until that promise settles and returns it.

187
00:11:41,910 --> 00:11:42,780
It's the result.

188
00:11:43,320 --> 00:11:45,630
So it's written clearly here.

189
00:11:45,900 --> 00:11:48,510
So this is the basic thing and.

190
00:11:49,730 --> 00:11:56,660
You can't use of it in regular functions, you need to use of it in the using function only only.

191
00:11:56,930 --> 00:12:03,260
OK, so I wouldn't want to work on the top level code, it needs to be inside the async.

192
00:12:03,290 --> 00:12:05,660
It will not work in the top low level code.

193
00:12:06,110 --> 00:12:12,860
OK, so there are a lot of things here and inside the async and a weird function that I try and get

194
00:12:13,220 --> 00:12:14,600
our best try and go.

195
00:12:14,610 --> 00:12:22,010
So let's see if we can that we can catch the error using try and go the same way we use regular true

196
00:12:22,250 --> 00:12:23,150
supply will.

197
00:12:24,350 --> 00:12:31,670
But I will run this code, and if this code like returns are properly resolved, the value so then will

198
00:12:31,670 --> 00:12:32,450
go get it.

199
00:12:32,720 --> 00:12:38,600
Otherwise, if we get an error, it'll catch the error and we'll pass that error inside the sketch function

200
00:12:39,530 --> 00:12:41,900
and then we can alert that error.

201
00:12:41,900 --> 00:12:43,340
We can console log data.

202
00:12:43,760 --> 00:12:48,980
So this was all about the async and from async functions.

203
00:12:48,980 --> 00:12:54,470
I'll wait and try and catch and promises so.
