Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,160 --> 00:00:01,920
welcome everyone to this tutorial all
2
00:00:01,920 --> 00:00:04,160
about async and await we are going to be
3
00:00:04,160 --> 00:00:06,000
exploring how these two keywords make
4
00:00:06,000 --> 00:00:08,160
asynchronous programming easier by using
5
00:00:08,160 --> 00:00:10,480
a lot of visual guides followed by code
6
00:00:10,480 --> 00:00:13,120
examples async and away are quite new to
7
00:00:13,120 --> 00:00:14,639
the world of javascript having been
8
00:00:14,639 --> 00:00:17,440
added in ecma script 2017 they
9
00:00:17,440 --> 00:00:18,960
essentially help promises look more
10
00:00:18,960 --> 00:00:20,720
readable by making it look like the
11
00:00:20,720 --> 00:00:23,119
old-school synchronous code think of
12
00:00:23,119 --> 00:00:25,439
them as syntactical sugar for promises
13
00:00:25,439 --> 00:00:26,560
if you will
14
00:00:26,560 --> 00:00:28,640
there are two parts to using async and
15
00:00:28,640 --> 00:00:31,119
await okay but let's look at the async
16
00:00:31,119 --> 00:00:33,600
keyword just after our video sponsors
17
00:00:33,600 --> 00:00:35,840
korea karma shout out who has made this
18
00:00:35,840 --> 00:00:38,239
video today possible korea karma is
19
00:00:38,239 --> 00:00:39,840
essentially a must-have for any aspiring
20
00:00:39,840 --> 00:00:41,840
developer as it is one of the fastest
21
00:00:41,840 --> 00:00:44,239
ways to break into a career in tech they
22
00:00:44,239 --> 00:00:46,239
offer boot camp advice as well as some
23
00:00:46,239 --> 00:00:48,480
immersive courses that are designed to
24
00:00:48,480 --> 00:00:50,399
teach you the skills to get hired
25
00:00:50,399 --> 00:00:52,480
quickly and i've helped over 1 million
26
00:00:52,480 --> 00:00:54,559
workers navigate their career each month
27
00:00:54,559 --> 00:00:56,399
through their community of like-minded
28
00:00:56,399 --> 00:00:58,480
dads mentors and coaches
29
00:00:58,480 --> 00:01:00,399
they even have live audio rooms in that
30
00:01:00,399 --> 00:01:02,559
app hosted by people like me so come
31
00:01:02,559 --> 00:01:04,720
join and hang out i have even been given
32
00:01:04,720 --> 00:01:06,479
a link for you in which 1 000 of my
33
00:01:06,479 --> 00:01:08,320
subscribers to sign up to queer karma
34
00:01:08,320 --> 00:01:10,400
will get exclusive early access to korea
35
00:01:10,400 --> 00:01:12,799
karma's new free mini coding bootcamp
36
00:01:12,799 --> 00:01:14,880
where you'll learn the basics of html
37
00:01:14,880 --> 00:01:18,159
css and javascript okay so thanks career
38
00:01:18,159 --> 00:01:19,439
karma
39
00:01:19,439 --> 00:01:21,840
now this video is part of a mini series
40
00:01:21,840 --> 00:01:23,520
that i am doing about asynchronous
41
00:01:23,520 --> 00:01:25,360
javascript and is in fact the last
42
00:01:25,360 --> 00:01:27,600
episode of this series if you haven't
43
00:01:27,600 --> 00:01:29,200
seen the others then please do check out
44
00:01:29,200 --> 00:01:30,720
the playlist that i have linked in the
45
00:01:30,720 --> 00:01:32,880
description below and as always i really
46
00:01:32,880 --> 00:01:34,640
do appreciate that sub as this is the
47
00:01:34,640 --> 00:01:36,400
way that i can continue to create free
48
00:01:36,400 --> 00:01:39,520
content for everyone here
49
00:01:39,520 --> 00:01:41,840
here we have a function just a standard
50
00:01:41,840 --> 00:01:44,000
super boring function
51
00:01:44,000 --> 00:01:44,799
now
52
00:01:44,799 --> 00:01:46,720
i'm going to change this into an async
53
00:01:46,720 --> 00:01:48,880
function how you ask
54
00:01:48,880 --> 00:01:51,680
simply by adding the keyword async
55
00:01:51,680 --> 00:01:54,720
before it just like that so just like
56
00:01:54,720 --> 00:01:56,799
that i've turned it into an async
57
00:01:56,799 --> 00:01:59,759
function but now if i run this code you
58
00:01:59,759 --> 00:02:03,119
will see a promise is returned
59
00:02:03,119 --> 00:02:06,640
it truly is an a sync function
60
00:02:06,640 --> 00:02:08,239
i can of course also write this as a
61
00:02:08,239 --> 00:02:10,479
function expression or in this case an
62
00:02:10,479 --> 00:02:12,879
async function expression so you would
63
00:02:12,879 --> 00:02:14,959
have seen me do this before if you watch
64
00:02:14,959 --> 00:02:17,040
my other tutorials i'm simply using this
65
00:02:17,040 --> 00:02:19,120
function and swapping around to a
66
00:02:19,120 --> 00:02:21,840
function expression by using the arrow
67
00:02:21,840 --> 00:02:25,040
function and assigning all of this to a
68
00:02:25,040 --> 00:02:26,560
const
69
00:02:26,560 --> 00:02:28,560
okay
70
00:02:28,560 --> 00:02:31,440
now if i was a core print as we just
71
00:02:31,440 --> 00:02:34,800
learned it would return a promise right
72
00:02:34,800 --> 00:02:37,360
from the previous video on promises we
73
00:02:37,360 --> 00:02:40,879
know that to consume the value returned
74
00:02:40,879 --> 00:02:43,519
when the promise fulfills we can use a
75
00:02:43,519 --> 00:02:45,280
then block
76
00:02:45,280 --> 00:02:47,840
so i would simply chain it
77
00:02:47,840 --> 00:02:49,599
so write then
78
00:02:49,599 --> 00:02:51,920
followed by i'm going to use an arrow
79
00:02:51,920 --> 00:02:54,080
function for this not a function
80
00:02:54,080 --> 00:02:55,280
there we go i'm just going to
81
00:02:55,280 --> 00:02:57,680
console.log the response
82
00:02:57,680 --> 00:03:01,040
simple how easy was that so to recap the
83
00:03:01,040 --> 00:03:03,920
async keyword is added to the function
84
00:03:03,920 --> 00:03:06,319
to tell our code that this is an async
85
00:03:06,319 --> 00:03:09,040
function and return a promise rather
86
00:03:09,040 --> 00:03:11,840
than returning a value but we still need
87
00:03:11,840 --> 00:03:14,400
to pair it with a weight
88
00:03:14,400 --> 00:03:16,239
let's move on to how we would use a
89
00:03:16,239 --> 00:03:18,319
weight in a similar way to make this
90
00:03:18,319 --> 00:03:20,560
code more readable
91
00:03:20,560 --> 00:03:23,200
a weight only works inside async
92
00:03:23,200 --> 00:03:26,159
functions within regular javascript code
93
00:03:26,159 --> 00:03:28,879
it can be put in front of any promise to
94
00:03:28,879 --> 00:03:31,680
pause your code in that line until the
95
00:03:31,680 --> 00:03:33,519
promise is fulfilled
96
00:03:33,519 --> 00:03:35,040
let's have a look at this
97
00:03:35,040 --> 00:03:36,799
encode
98
00:03:36,799 --> 00:03:38,480
for this i'm going to go back to a
99
00:03:38,480 --> 00:03:41,120
previous code example i used in the
100
00:03:41,120 --> 00:03:43,760
video about promises in which we make a
101
00:03:43,760 --> 00:03:46,239
fetch request to the countries api and
102
00:03:46,239 --> 00:03:48,239
rewrite it with the knowledge we have to
103
00:03:48,239 --> 00:03:50,640
make everything more readable
104
00:03:50,640 --> 00:03:52,159
i'm just going to keep this up here so
105
00:03:52,159 --> 00:03:54,480
you can compare the two
106
00:03:54,480 --> 00:03:57,519
so as a recap what is happening here if
107
00:03:57,519 --> 00:03:58,799
you remember or if you watched the
108
00:03:58,799 --> 00:04:00,879
previous video is that we change some
109
00:04:00,879 --> 00:04:02,720
promises together
110
00:04:02,720 --> 00:04:06,239
this fetch here returns a promise we
111
00:04:06,239 --> 00:04:08,879
then chained then to it and then call
112
00:04:08,879 --> 00:04:11,599
this function when that promise resolved
113
00:04:11,599 --> 00:04:13,760
then we've returned another promise and
114
00:04:13,760 --> 00:04:16,000
waited for that one to resolve and then
115
00:04:16,000 --> 00:04:18,799
ended it with a catch to catch all the
116
00:04:18,799 --> 00:04:19,919
errors
117
00:04:19,919 --> 00:04:22,240
it looks fine but let's get to using our
118
00:04:22,240 --> 00:04:24,720
new async n08 keywords to really clean
119
00:04:24,720 --> 00:04:26,840
it up and make it way more
120
00:04:26,840 --> 00:04:29,120
readable so the first thing i'm going to
121
00:04:29,120 --> 00:04:30,720
do is write a function and i'm going to
122
00:04:30,720 --> 00:04:32,400
call it getdata
123
00:04:32,400 --> 00:04:34,639
now let's make asynchronous based on
124
00:04:34,639 --> 00:04:36,320
what we have learnt
125
00:04:36,320 --> 00:04:38,639
so how do we do this again
126
00:04:38,639 --> 00:04:41,759
that is right we simply write async in
127
00:04:41,759 --> 00:04:42,880
front of it
128
00:04:42,880 --> 00:04:45,600
so now we have an async function this is
129
00:04:45,600 --> 00:04:47,600
going to return a promise
130
00:04:47,600 --> 00:04:50,080
as we know from before
131
00:04:50,080 --> 00:04:52,880
so let's try this out and call getdata
132
00:04:52,880 --> 00:04:56,720
and wrap it in a console.log
133
00:04:56,720 --> 00:04:58,000
okay
134
00:04:58,000 --> 00:04:58,880
and
135
00:04:58,880 --> 00:05:00,479
there we
136
00:05:00,479 --> 00:05:04,160
go a promise is being returned
137
00:05:04,160 --> 00:05:07,360
now we need to use a weight inside of it
138
00:05:07,360 --> 00:05:09,680
this time i'm going to use fetch again
139
00:05:09,680 --> 00:05:12,080
but instead of chaining it with then i'm
140
00:05:12,080 --> 00:05:13,919
going to stick the word away in front of
141
00:05:13,919 --> 00:05:14,800
it
142
00:05:14,800 --> 00:05:17,680
this await essentially stops the fetch
143
00:05:17,680 --> 00:05:20,479
from assigning a value to the response
144
00:05:20,479 --> 00:05:22,960
until there is a response to be given or
145
00:05:22,960 --> 00:05:25,440
in other words until the promise from
146
00:05:25,440 --> 00:05:27,919
fetch has resolved
147
00:05:27,919 --> 00:05:29,759
now we are not yet done
148
00:05:29,759 --> 00:05:32,160
in the previous example here we had
149
00:05:32,160 --> 00:05:34,080
another promise right
150
00:05:34,080 --> 00:05:37,199
that's why we changed the then here
151
00:05:37,199 --> 00:05:39,840
we need to deal with this second promise
152
00:05:39,840 --> 00:05:41,600
so in other words we need to deal with
153
00:05:41,600 --> 00:05:43,840
the return of what happens when we grab
154
00:05:43,840 --> 00:05:47,039
response and use the json method which
155
00:05:47,039 --> 00:05:49,759
is also asynchronous which means it also
156
00:05:49,759 --> 00:05:51,520
returns a promise
157
00:05:51,520 --> 00:05:52,479
so
158
00:05:52,479 --> 00:05:55,360
how do you think we should do that
159
00:05:55,360 --> 00:05:58,319
a clue is that we just did it
160
00:05:58,319 --> 00:06:00,960
so once again we would grab the response
161
00:06:00,960 --> 00:06:04,080
and use the json method on it to get the
162
00:06:04,080 --> 00:06:05,360
data
163
00:06:05,360 --> 00:06:08,000
now let's console.log the data and call
164
00:06:08,000 --> 00:06:11,600
the async function of getdata
165
00:06:11,600 --> 00:06:14,160
wonderful
166
00:06:15,120 --> 00:06:17,199
okay so let's just do that
167
00:06:17,199 --> 00:06:18,840
here
168
00:06:18,840 --> 00:06:22,319
wonderful but we are not yet done
169
00:06:22,319 --> 00:06:24,240
we want to be able to use this data
170
00:06:24,240 --> 00:06:26,880
outside of the function so let's go
171
00:06:26,880 --> 00:06:29,280
ahead and return it so when i call the
172
00:06:29,280 --> 00:06:31,440
function this will get
173
00:06:31,440 --> 00:06:33,120
returned
174
00:06:33,120 --> 00:06:36,880
but once again this is not enough
175
00:06:36,880 --> 00:06:37,840
why
176
00:06:37,840 --> 00:06:40,160
surely if we call get data it will
177
00:06:40,160 --> 00:06:43,440
return the data right well wrong
178
00:06:43,440 --> 00:06:46,240
get data is an async function and what
179
00:06:46,240 --> 00:06:48,880
do we know about async functions they
180
00:06:48,880 --> 00:06:51,199
return promises
181
00:06:51,199 --> 00:06:53,280
so now i'm actually going to go back to
182
00:06:53,280 --> 00:06:55,759
using then for this so i'm going to
183
00:06:55,759 --> 00:06:59,199
chain the get data with a callback that
184
00:06:59,199 --> 00:07:00,960
looks like
185
00:07:00,960 --> 00:07:03,520
this we chain this
186
00:07:03,520 --> 00:07:05,919
as it is outside now of our async
187
00:07:05,919 --> 00:07:09,039
functions we can't use a weight anymore
188
00:07:09,039 --> 00:07:11,440
a weight needs to be inside the async
189
00:07:11,440 --> 00:07:14,160
function as we said at the beginning
190
00:07:14,160 --> 00:07:15,520
okay
191
00:07:15,520 --> 00:07:18,560
so this is what the code will now look
192
00:07:18,560 --> 00:07:19,599
like
193
00:07:19,599 --> 00:07:23,280
we are now chaining using then and we
194
00:07:23,280 --> 00:07:26,000
can of course also catch the errors
195
00:07:26,000 --> 00:07:28,560
again by chaining the
196
00:07:28,560 --> 00:07:30,880
catch and logging out the
197
00:07:30,880 --> 00:07:32,800
errors
198
00:07:32,800 --> 00:07:33,919
and
199
00:07:33,919 --> 00:07:35,039
wonderful
200
00:07:35,039 --> 00:07:36,880
i hope you've learned a lot in this
201
00:07:36,880 --> 00:07:39,120
video i hope it was useful to you and
202
00:07:39,120 --> 00:07:40,880
please do not forget that if something
203
00:07:40,880 --> 00:07:43,120
doesn't make sense please do comment in
204
00:07:43,120 --> 00:07:45,199
the description in the comments below as
205
00:07:45,199 --> 00:07:47,039
it really is my goal to help you
206
00:07:47,039 --> 00:07:49,520
understand all about asynchronous
207
00:07:49,520 --> 00:07:51,840
javascript thanks very much for watching
208
00:07:51,840 --> 00:07:53,599
this mini series i hope you can see the
209
00:07:53,599 --> 00:07:55,120
difference in how we progress from the
210
00:07:55,120 --> 00:07:57,360
beginning video to this very end one
211
00:07:57,360 --> 00:07:58,879
once again all the videos will be
212
00:07:58,879 --> 00:08:01,039
available in the description below as a
213
00:08:01,039 --> 00:08:04,440
little miniseries15028
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.