1
00:00:00,570 --> 00:00:06,540
Here you can see once again, I open my Visual Studio Code editor and my Chrome browser, and I already

2
00:00:06,540 --> 00:00:10,480
create an async function named test, which is empty.

3
00:00:10,500 --> 00:00:14,280
So first I'm going to print a statement outside the function.

4
00:00:14,860 --> 00:00:16,350
Console.log.

5
00:00:16,350 --> 00:00:20,940
Inside the parentheses one colon message.

6
00:00:21,510 --> 00:00:22,200
It's mean.

7
00:00:22,200 --> 00:00:23,940
This is our first message.

8
00:00:24,360 --> 00:00:27,750
Here we are going to understand how await function worked.

9
00:00:27,750 --> 00:00:32,490
And I'm going to copy this same message and paste it inside the async function.

10
00:00:32,880 --> 00:00:35,010
And I'm going to duplicate it two times.

11
00:00:35,960 --> 00:00:40,100
And also I'm going to change their ID to three and four.

12
00:00:40,130 --> 00:00:43,430
So first it's going to print our message one statement.

13
00:00:43,460 --> 00:00:48,050
Then it's going to call our function test which is a async function.

14
00:00:48,080 --> 00:00:51,650
Then it print our three statements inside the async function.

15
00:00:51,650 --> 00:00:56,030
And once again I'm going to print in our console another message message five.

16
00:00:56,300 --> 00:01:01,880
If I save this file you can see in my console it prints all the messages serially.

17
00:01:01,910 --> 00:01:02,660
Messi's one.

18
00:01:02,660 --> 00:01:03,110
Messi's two.

19
00:01:03,140 --> 00:01:04,100
Messi's three.

20
00:01:04,130 --> 00:01:04,700
Messi's four.

21
00:01:04,700 --> 00:01:05,650
Messi's five.

22
00:01:05,660 --> 00:01:06,830
From 2 to 4.

23
00:01:06,860 --> 00:01:09,200
They are all from async function.

24
00:01:09,320 --> 00:01:12,590
And now I'm going to type await method before the message.

25
00:01:12,590 --> 00:01:16,040
Three I already told you what is the meaning of await.

26
00:01:16,070 --> 00:01:19,760
Wait and complete the job until this work is done.

27
00:01:19,760 --> 00:01:21,950
Complete our another work.

28
00:01:21,980 --> 00:01:24,770
Then it's return to our await function and check.

29
00:01:24,800 --> 00:01:26,480
Is it finished or not?

30
00:01:26,510 --> 00:01:29,810
If finished then it print Messi's four.

31
00:01:29,810 --> 00:01:32,000
So I'm going to save this file once again.

32
00:01:32,150 --> 00:01:34,790
And you can see it's changed our sequence.

33
00:01:35,000 --> 00:01:37,710
You can see first it print Messi's one.

34
00:01:37,730 --> 00:01:41,840
Then I call our function test which is a async function.

35
00:01:41,840 --> 00:01:44,510
So it print his first statement two.

36
00:01:44,540 --> 00:01:48,350
Then it print await message main message three.

37
00:01:48,350 --> 00:01:55,550
But I also declare our function to wait as long as it is waiting, the message outside the test function

38
00:01:55,550 --> 00:01:56,990
will be printed.

39
00:01:56,990 --> 00:02:00,350
So after three it's print five, not four.

40
00:02:00,380 --> 00:02:05,030
As soon as the job is done, it came back again to check.

41
00:02:05,060 --> 00:02:07,070
Is it work properly or not?

42
00:02:07,100 --> 00:02:11,060
If it works properly then print Messi's four.

43
00:02:11,060 --> 00:02:15,110
So in that way our await method work in our async function.

44
00:02:15,110 --> 00:02:20,150
If we try to run await method without async function, then it's return error.

45
00:02:20,180 --> 00:02:26,840
And now I'm going to show you what is the actual use of await method, where and why we use await method.

46
00:02:26,840 --> 00:02:32,300
Whenever we try to face data from the server, then we need to use await method.

47
00:02:32,300 --> 00:02:37,520
So here I'm going to use fetch method and we learn about it in our previous video.

48
00:02:37,520 --> 00:02:39,710
And here I'm going to fetch a Json data.

49
00:02:39,710 --> 00:02:43,670
And I already created Json file in my current working directory.

50
00:02:43,670 --> 00:02:47,750
Let me show you here you can see I create a folder named contain.

51
00:02:47,900 --> 00:02:52,790
And inside this folder you can see a Json file named student dot Json.

52
00:02:53,000 --> 00:02:57,170
And we have three records in our file name age and city.

53
00:02:57,200 --> 00:02:58,820
It's a Json format.

54
00:02:58,850 --> 00:03:04,280
Also, you can retrieve fake Json data from website basically for testing purpose.

55
00:03:04,280 --> 00:03:07,040
So let's back to the index.html file.

56
00:03:07,040 --> 00:03:09,230
And now I'm going to use the fetch method.

57
00:03:09,680 --> 00:03:11,930
So first I'm going to create a variable.

58
00:03:11,930 --> 00:03:18,380
And I'm going to remove this line const response equal to fetch.

59
00:03:19,100 --> 00:03:20,620
And insert the fetch method.

60
00:03:20,630 --> 00:03:26,630
I'm going to pass my URL content slash student dot Json.

61
00:03:26,630 --> 00:03:32,930
So before this face function I'm going to use our method await its mean.

62
00:03:32,930 --> 00:03:40,480
I told my function to wait and fetch the complete data from our Json file until the process complete.

63
00:03:40,490 --> 00:03:42,290
It's run our next codes.

64
00:03:42,410 --> 00:03:45,050
As soon as all work is over.

65
00:03:45,050 --> 00:03:49,820
It's written to the await function and continue with next lines.

66
00:03:50,510 --> 00:03:53,630
We know our fetch method return a promise.

67
00:03:53,630 --> 00:03:55,670
So I'm going to create a variable.

68
00:03:55,940 --> 00:03:58,040
But first I'm going to remove this line.

69
00:03:59,310 --> 00:04:02,160
Const student equal to.

70
00:04:02,160 --> 00:04:06,970
And once again I'm going to use await method response dot Json.

71
00:04:06,990 --> 00:04:09,600
And we learn about it in our previous video.

72
00:04:09,930 --> 00:04:14,490
Basically our fetch method returned Json or text format.

73
00:04:14,640 --> 00:04:17,270
So in response we get Json file.

74
00:04:17,280 --> 00:04:19,590
So we need to use dot Json.

75
00:04:19,590 --> 00:04:24,210
And I'm going to return all the data which I retrieved from the Json file.

76
00:04:24,780 --> 00:04:28,800
Return student and return student value.

77
00:04:28,980 --> 00:04:32,070
And we know our async function return a promise.

78
00:04:32,070 --> 00:04:34,470
So first is going to print message one.

79
00:04:34,470 --> 00:04:37,050
And then it going to run my function test.

80
00:04:37,200 --> 00:04:38,640
It's a async function.

81
00:04:38,640 --> 00:04:40,860
So it's going to print message to.

82
00:04:41,190 --> 00:04:43,650
After that we are asking to wait here.

83
00:04:43,800 --> 00:04:48,000
After our first await function I'm going to print another message.

84
00:04:48,240 --> 00:04:53,250
Message three and also I'm going to create a variable for our function call.

85
00:04:54,040 --> 00:04:57,190
Let x equal to test.

86
00:04:57,670 --> 00:05:00,310
And I want to print this response in our console.

87
00:05:00,850 --> 00:05:05,620
So I'm going to type console dot log inside the round braces x.

88
00:05:05,950 --> 00:05:07,270
Let's save the code.

89
00:05:07,300 --> 00:05:09,010
You can see in my console.

90
00:05:09,640 --> 00:05:12,610
As you can see first it print matches one.

91
00:05:12,700 --> 00:05:15,490
And then we call our function test.

92
00:05:16,210 --> 00:05:18,010
So it's print masses too.

93
00:05:18,190 --> 00:05:19,930
And then I told it to wait.

94
00:05:20,140 --> 00:05:22,690
Here I am going to fetch data from server.

95
00:05:22,870 --> 00:05:27,680
So it's jump out of this function and started working on masses for.

96
00:05:27,700 --> 00:05:32,040
So you can see in my console section it prints four after two.

97
00:05:32,050 --> 00:05:36,310
And when masses four is complete we call our x variable.

98
00:05:36,310 --> 00:05:38,620
So it's going back to the async function.

99
00:05:38,620 --> 00:05:40,720
And it gets a response from server.

100
00:05:40,720 --> 00:05:44,470
And you can see it print our response as a promise.

101
00:05:44,470 --> 00:05:47,410
And you can see we have two array in our promise.

102
00:05:47,830 --> 00:05:50,190
And our promise state is fulfilled.

103
00:05:50,200 --> 00:05:53,770
After complete the promise section then it print our message.

104
00:05:53,770 --> 00:05:59,170
Three in that way we use async and await method with our fetch function.

105
00:05:59,170 --> 00:06:02,140
And now I'm going to remove all the extra messages.

106
00:06:02,470 --> 00:06:05,020
So I'm going to remove all the console section.

107
00:06:05,650 --> 00:06:07,250
Now it's looking pretty simple.

108
00:06:07,270 --> 00:06:10,330
First we run await function for our fetch content.

109
00:06:10,540 --> 00:06:14,590
It will keep waiting until the data comes from the server.

110
00:06:14,590 --> 00:06:16,900
And we told our response to wait once again.

111
00:06:16,900 --> 00:06:19,030
So I use another await method.

112
00:06:19,060 --> 00:06:20,830
Then we return our result.

113
00:06:20,860 --> 00:06:23,290
If I save this file, you can see in my console.

114
00:06:23,290 --> 00:06:29,410
As a result, we get a promise and we have two array in our promise because in our Json file we have

115
00:06:29,410 --> 00:06:30,070
two result.

116
00:06:30,220 --> 00:06:34,210
And as you know we can use then and catch function with promise method.

117
00:06:34,720 --> 00:06:37,150
So first I'm going to remove the console section.

118
00:06:37,150 --> 00:06:40,090
And also I'm going to remove this variable x.

119
00:06:40,240 --> 00:06:42,480
So first we call our test function.

120
00:06:42,490 --> 00:06:47,230
If it return promise successfully then we are going to use then function.

121
00:06:47,970 --> 00:06:51,000
And insert the then function, I'm going to create an arrow function.

122
00:06:51,970 --> 00:06:53,950
And also I'm going to create cash method.

123
00:06:53,980 --> 00:07:02,440
After that dot cash inside the parentheses I'm going to create another arrow function.

124
00:07:02,440 --> 00:07:07,450
If there is an error in our function then automatically it run catch function.

125
00:07:07,450 --> 00:07:10,360
And now I'm going to save student record in a variable.

126
00:07:10,360 --> 00:07:12,900
And our variable name is r e s.

127
00:07:12,910 --> 00:07:14,800
Data means response data.

128
00:07:14,800 --> 00:07:20,410
And I'm going to print this value in our console console dot log r e s data.

129
00:07:20,410 --> 00:07:26,500
And if there is an error in our code our catch method going to print another message error message.

130
00:07:26,500 --> 00:07:32,410
So I'm going to create another variable error inside the error function I'm going to print console dot

131
00:07:32,410 --> 00:07:33,930
log error.

132
00:07:33,940 --> 00:07:36,820
So first we called our async function test.

133
00:07:36,850 --> 00:07:38,560
And it return a promise.

134
00:07:38,590 --> 00:07:42,010
If promise is fulfilled then it run then function.

135
00:07:42,250 --> 00:07:46,990
And if there is an error in our function, it automatically run catch function.

136
00:07:46,990 --> 00:07:51,310
And if I save this file you can see in my console it returns an array.

137
00:07:51,310 --> 00:07:55,990
And if I open this array you can see it provides Json object.

138
00:07:56,080 --> 00:07:57,790
And if I change file path.

139
00:07:57,790 --> 00:08:02,050
So I'm going to remove S from student and save this file.

140
00:08:02,050 --> 00:08:07,330
You can see in my console it print an error for zero four means file not found.

141
00:08:07,810 --> 00:08:10,750
So it run our catch method and print the error.

142
00:08:11,260 --> 00:08:16,360
Now let's see how we can convert so many lines in a one line.

143
00:08:16,690 --> 00:08:18,580
I just want to make it one liner.

144
00:08:18,790 --> 00:08:21,400
So first I'm going to comment out all this line.

145
00:08:21,760 --> 00:08:24,820
Mainly we are focused on fetch and await method.

146
00:08:25,510 --> 00:08:31,390
So I'm going to return inside the parentheses I'm going to call await method.

147
00:08:31,930 --> 00:08:34,090
And I want to return Json format.

148
00:08:34,090 --> 00:08:36,430
So I use dot Json function.

149
00:08:36,430 --> 00:08:38,440
So we make it one liner.

150
00:08:38,590 --> 00:08:41,340
If I save this file you can see in my console.

151
00:08:41,350 --> 00:08:46,000
Similarly it returns an array and we have two Json object inside this array.

152
00:08:46,000 --> 00:08:50,320
So here you can see I convert three line of code into a one line.

153
00:08:50,590 --> 00:08:53,830
Let's talk about our last topic error handling.

154
00:08:53,950 --> 00:08:57,340
Basically we handle our error using catch function.

155
00:08:57,520 --> 00:09:00,130
There is a drawback using catch function.

156
00:09:00,130 --> 00:09:05,710
When we call test function every time we need to use catch method to handle the error.

157
00:09:06,130 --> 00:09:08,320
So I don't want to use every time.

158
00:09:08,320 --> 00:09:10,660
So there is a shortcut to handle it.

159
00:09:10,870 --> 00:09:16,360
So I'm going to uncomment all the lines and comment out this return section.

160
00:09:16,840 --> 00:09:20,500
Basically we use try or catch to handle this error.

161
00:09:21,010 --> 00:09:24,940
So inside the async function we use try or catch block.

162
00:09:25,940 --> 00:09:32,270
In our next video, we will learn more about try or catch inside the try block.

163
00:09:32,270 --> 00:09:34,010
We type our whole code.

164
00:09:34,370 --> 00:09:37,190
So I'm going to move this code inside the try block.

165
00:09:37,460 --> 00:09:38,210
It's mean.

166
00:09:38,210 --> 00:09:39,470
We are trying this code.

167
00:09:39,530 --> 00:09:44,840
If it worked properly then it return and we can access this in our then function.

168
00:09:45,020 --> 00:09:50,060
And if there is an error in this try block then it's automatically run catch block.

169
00:09:50,060 --> 00:09:52,820
And inside the catch block we print our error.

170
00:09:52,970 --> 00:09:56,270
So I'm going to move this error message in this block.

171
00:09:56,270 --> 00:09:58,340
And I'm going to remove the catch method.

172
00:09:58,340 --> 00:10:02,780
And also we need to declare the error variable inside the error block.

173
00:10:03,140 --> 00:10:09,350
So whenever we call test function then we don't need to type catch method to handle the error.

174
00:10:09,860 --> 00:10:14,420
And if I save this file you can see in my console we can access our data successfully.

175
00:10:14,510 --> 00:10:17,420
And if I create an error inside my function.

176
00:10:17,420 --> 00:10:18,980
So I'm going to change the response.

177
00:10:18,980 --> 00:10:23,180
Spelling is double to poncey.

178
00:10:23,390 --> 00:10:25,850
And you can see there is no variable name.

179
00:10:25,850 --> 00:10:30,080
Rest 22 pawns and we know it's going to return an error.

180
00:10:30,080 --> 00:10:32,870
In that case it will run our catch block.

181
00:10:32,870 --> 00:10:40,310
And if I save this file you can see an error in my console it print reference error rest 22 pawns is

182
00:10:40,310 --> 00:10:46,580
not defined at test, because we do not define this variable in our function and also it print another

183
00:10:46,580 --> 00:10:48,290
message undefined.

184
00:10:48,290 --> 00:10:52,340
So you can see how we can handle error using try or catch block.

185
00:10:52,370 --> 00:10:56,840
It is pretty similar with try except condition in Python.

186
00:10:57,080 --> 00:11:03,080
If there is an error in our try block, then it automatically jump to the catch block and it print our

187
00:11:03,080 --> 00:11:04,130
error message.

188
00:11:04,400 --> 00:11:07,790
And I'm going to resolve this problem and save this file once again.

189
00:11:07,790 --> 00:11:09,650
And now it's worked pretty fine.

190
00:11:09,650 --> 00:11:15,380
If you want to print this result in your document section you can you can use for loop for that.

191
00:11:15,380 --> 00:11:22,160
So I hope now it's clear for you what is async and await method and how we can use it with our fetch

192
00:11:22,160 --> 00:11:22,760
method.

193
00:11:22,790 --> 00:11:24,290
Thanks for watching this video.

194
00:11:24,290 --> 00:11:25,100
See you soon!
