Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,880 --> 00:00:03,120
hello and welcome to this video about
2
00:00:03,120 --> 00:00:06,080
asynchronous callbacks there are two
3
00:00:06,080 --> 00:00:07,759
types of main styles that you will come
4
00:00:07,759 --> 00:00:08,160
across
5
00:00:08,160 --> 00:00:10,880
in asynchronous code they are the older
6
00:00:10,880 --> 00:00:12,000
style callbacks
7
00:00:12,000 --> 00:00:14,960
and the newer style promises whilst
8
00:00:14,960 --> 00:00:17,279
callbacks are considered to be the older
9
00:00:17,279 --> 00:00:17,920
type
10
00:00:17,920 --> 00:00:19,600
it is still important to get to know
11
00:00:19,600 --> 00:00:21,359
them case you come across them
12
00:00:21,359 --> 00:00:23,680
on your journey to become a well-rounded
13
00:00:23,680 --> 00:00:24,880
developer
14
00:00:24,880 --> 00:00:27,279
async callbacks are functions that are
15
00:00:27,279 --> 00:00:28,160
specified as
16
00:00:28,160 --> 00:00:30,560
arguments when calling another function
17
00:00:30,560 --> 00:00:31,599
that will get to work
18
00:00:31,599 --> 00:00:34,719
executing code in the background only
19
00:00:34,719 --> 00:00:35,280
when the
20
00:00:35,280 --> 00:00:37,840
background code finishes running the
21
00:00:37,840 --> 00:00:38,640
callback
22
00:00:38,640 --> 00:00:41,360
is called don't worry if none of this
23
00:00:41,360 --> 00:00:42,879
makes sense because we're going to go
24
00:00:42,879 --> 00:00:43,360
through this
25
00:00:43,360 --> 00:00:47,039
step by step with code an example
26
00:00:47,039 --> 00:00:49,440
of an async callback function you might
27
00:00:49,440 --> 00:00:50,719
have come across before
28
00:00:50,719 --> 00:00:53,039
is when you use the add event listener
29
00:00:53,039 --> 00:00:54,079
javascript
30
00:00:54,079 --> 00:00:57,680
method the second parameter here
31
00:00:57,680 --> 00:00:59,760
of the ad event listener javascript
32
00:00:59,760 --> 00:01:01,680
method so right here
33
00:01:01,680 --> 00:01:04,960
is considered to be an async callback
34
00:01:04,960 --> 00:01:07,119
the method works in such a way under the
35
00:01:07,119 --> 00:01:09,680
hood that if we pass an event throughout
36
00:01:09,680 --> 00:01:11,439
the first parameter
37
00:01:11,439 --> 00:01:14,240
this in this case it is a click but it
38
00:01:14,240 --> 00:01:16,320
is one of many from a long list that
39
00:01:16,320 --> 00:01:17,360
exists
40
00:01:17,360 --> 00:01:20,000
the event listens out for this event and
41
00:01:20,000 --> 00:01:21,840
once it hears the event
42
00:01:21,840 --> 00:01:24,960
the second parameter so in this case the
43
00:01:24,960 --> 00:01:26,320
callback function
44
00:01:26,320 --> 00:01:29,040
is called when we pass a callback
45
00:01:29,040 --> 00:01:30,400
function as an argument
46
00:01:30,400 --> 00:01:33,200
to another function or method we are
47
00:01:33,200 --> 00:01:34,880
only passing the function's
48
00:01:34,880 --> 00:01:37,759
reference as an argument i.e the
49
00:01:37,759 --> 00:01:40,000
callback function is not executed
50
00:01:40,000 --> 00:01:42,600
immediately it is called back
51
00:01:42,600 --> 00:01:43,920
asynchronously
52
00:01:43,920 --> 00:01:45,680
somewhere inside the containing
53
00:01:45,680 --> 00:01:47,119
function's body
54
00:01:47,119 --> 00:01:49,360
the containing function is responsible
55
00:01:49,360 --> 00:01:51,840
for executing the callback function
56
00:01:51,840 --> 00:01:54,960
when the time comes so
57
00:01:54,960 --> 00:01:56,560
that is essentially what is happening
58
00:01:56,560 --> 00:01:58,960
all under the hood of the add event
59
00:01:58,960 --> 00:01:59,680
listener
60
00:01:59,680 --> 00:02:02,560
method i could of course also written
61
00:02:02,560 --> 00:02:04,880
like this so just taken out this entire
62
00:02:04,880 --> 00:02:05,840
function
63
00:02:05,840 --> 00:02:08,879
and assigned it to the const alert
64
00:02:08,879 --> 00:02:11,840
me so that is a function expression or
65
00:02:11,840 --> 00:02:13,840
as you might have seen it written i mean
66
00:02:13,840 --> 00:02:15,200
this is just a function
67
00:02:15,200 --> 00:02:17,520
you could also just write function alert
68
00:02:17,520 --> 00:02:19,599
me like so so just the traditional way
69
00:02:19,599 --> 00:02:20,959
of writing a function
70
00:02:20,959 --> 00:02:23,440
okay all of these are essentially the
71
00:02:23,440 --> 00:02:25,200
same it is a contained function
72
00:02:25,200 --> 00:02:28,560
it is not being called so now you can
73
00:02:28,560 --> 00:02:29,280
see that
74
00:02:29,280 --> 00:02:33,120
alert me is essentially equivalent to
75
00:02:33,120 --> 00:02:36,480
this okay cons alert
76
00:02:36,480 --> 00:02:40,879
parentheses our function curly braces
77
00:02:40,879 --> 00:02:45,599
allow me equals parentheses curly braces
78
00:02:45,599 --> 00:02:47,760
okay and that is of course what we are
79
00:02:47,760 --> 00:02:49,599
passing into the ad
80
00:02:49,599 --> 00:02:53,040
event listener okay i like to think of
81
00:02:53,040 --> 00:02:54,000
it as this
82
00:02:54,000 --> 00:02:56,239
like here is a function it is just
83
00:02:56,239 --> 00:02:57,840
bursting with all the power
84
00:02:57,840 --> 00:02:59,920
of a function but can't do anything with
85
00:02:59,920 --> 00:03:01,200
it yet until
86
00:03:01,200 --> 00:03:04,480
it is called and it just called inside
87
00:03:04,480 --> 00:03:05,200
the method
88
00:03:05,200 --> 00:03:08,239
under the hood or inside a function
89
00:03:08,239 --> 00:03:10,879
cool to understand this a little bit
90
00:03:10,879 --> 00:03:11,440
better
91
00:03:11,440 --> 00:03:13,120
let's actually write our own function
92
00:03:13,120 --> 00:03:14,800
one where we can see actually what is
93
00:03:14,800 --> 00:03:15,440
going on
94
00:03:15,440 --> 00:03:19,200
under the hood so here i have written an
95
00:03:19,200 --> 00:03:20,560
example i'm just going to
96
00:03:20,560 --> 00:03:24,319
paste this in here here is an example
97
00:03:24,319 --> 00:03:28,239
that loads a resource by the xml http
98
00:03:28,239 --> 00:03:29,280
request
99
00:03:29,280 --> 00:03:32,000
api so this is going back to our first
100
00:03:32,000 --> 00:03:32,799
ajax
101
00:03:32,799 --> 00:03:35,120
lesson the first lesson of this mini
102
00:03:35,120 --> 00:03:37,040
series
103
00:03:37,040 --> 00:03:39,840
so what is going on here is that we
104
00:03:39,840 --> 00:03:41,200
essentially have
105
00:03:41,200 --> 00:03:44,799
two functions so function show image
106
00:03:44,799 --> 00:03:47,920
and function create image we then
107
00:03:47,920 --> 00:03:51,120
use the show image function and
108
00:03:51,120 --> 00:03:55,760
call it with these three parameters
109
00:03:55,760 --> 00:03:57,840
so this means that i'm essentially
110
00:03:57,840 --> 00:03:59,519
getting apple jpeg
111
00:03:59,519 --> 00:04:03,439
and passing onto the url so every time i
112
00:04:03,439 --> 00:04:04,400
use url
113
00:04:04,400 --> 00:04:07,680
in this function i'm essentially meaning
114
00:04:07,680 --> 00:04:10,640
apple jpeg same for blob so when we pass
115
00:04:10,640 --> 00:04:11,040
through
116
00:04:11,040 --> 00:04:14,640
blob as the type the response type
117
00:04:14,640 --> 00:04:17,120
is going to take blob as a string
118
00:04:17,120 --> 00:04:18,639
response type takes many different
119
00:04:18,639 --> 00:04:20,320
things please do some research on this
120
00:04:20,320 --> 00:04:22,000
if you wish but we are
121
00:04:22,000 --> 00:04:24,400
essentially passing through blob as the
122
00:04:24,400 --> 00:04:26,080
type for the response
123
00:04:26,080 --> 00:04:29,120
type and same for create image
124
00:04:29,120 --> 00:04:30,720
so this is interesting this is the
125
00:04:30,720 --> 00:04:33,280
callback you can see the function create
126
00:04:33,280 --> 00:04:34,400
image here
127
00:04:34,400 --> 00:04:37,199
and i am essentially passing it through
128
00:04:37,199 --> 00:04:38,560
into this function
129
00:04:38,560 --> 00:04:41,120
so this is it now this is create image
130
00:04:41,120 --> 00:04:43,040
and i'm going to be using it here
131
00:04:43,040 --> 00:04:46,000
okay but i'm only going to be using it
132
00:04:46,000 --> 00:04:48,240
after
133
00:04:48,240 --> 00:04:51,520
all of this has completed by this i mean
134
00:04:51,520 --> 00:04:53,199
that essentially we have the
135
00:04:53,199 --> 00:04:55,680
xml http request object here i'm getting
136
00:04:55,680 --> 00:04:56,560
a new one
137
00:04:56,560 --> 00:04:59,040
and then saving as the cons xhr so i can
138
00:04:59,040 --> 00:05:00,080
use these methods
139
00:05:00,080 --> 00:05:03,280
on it so essentially what i am doing is
140
00:05:03,280 --> 00:05:03,919
waiting
141
00:05:03,919 --> 00:05:07,039
for my apple image to
142
00:05:07,039 --> 00:05:09,919
uh be gotten for this file so that i can
143
00:05:09,919 --> 00:05:10,560
use it
144
00:05:10,560 --> 00:05:13,680
only once i have the apple image
145
00:05:13,680 --> 00:05:16,560
i can then pass through the response
146
00:05:16,560 --> 00:05:19,199
into the callback
147
00:05:19,199 --> 00:05:21,840
which is this so xhr response is going
148
00:05:21,840 --> 00:05:22,560
here
149
00:05:22,560 --> 00:05:25,919
here and then i am displaying the apple
150
00:05:25,919 --> 00:05:28,479
okay so this is a great example of how
151
00:05:28,479 --> 00:05:31,360
to use a callback because i only want to
152
00:05:31,360 --> 00:05:34,720
display or create the image once
153
00:05:34,720 --> 00:05:40,560
all of this has been completed
154
00:05:40,800 --> 00:05:44,080
cool hopefully that makes
155
00:05:44,080 --> 00:05:48,000
sense once again one function
156
00:05:48,000 --> 00:05:51,199
another function i am then passing this
157
00:05:51,199 --> 00:05:52,639
function through
158
00:05:52,639 --> 00:05:56,240
into this function to be used as a
159
00:05:56,240 --> 00:05:57,680
callback
160
00:05:57,680 --> 00:06:00,080
please do note that not all callbacks
161
00:06:00,080 --> 00:06:01,360
are asynchronous
162
00:06:01,360 --> 00:06:04,400
some run synchronicity an example is
163
00:06:04,400 --> 00:06:05,759
when we use
164
00:06:05,759 --> 00:06:07,919
for each to loop through items in an
165
00:06:07,919 --> 00:06:08,800
array
166
00:06:08,800 --> 00:06:12,560
sort of like this in this example we are
167
00:06:12,560 --> 00:06:14,000
just looping through an array
168
00:06:14,000 --> 00:06:15,680
and we are just printing out values to
169
00:06:15,680 --> 00:06:17,199
the console the
170
00:06:17,199 --> 00:06:18,880
argument that we are passing into the
171
00:06:18,880 --> 00:06:20,479
for each is of course
172
00:06:20,479 --> 00:06:22,479
a callback function because you see the
173
00:06:22,479 --> 00:06:23,520
parenthesis
174
00:06:23,520 --> 00:06:26,000
the arrow function and you know the kali
175
00:06:26,000 --> 00:06:27,520
braces
176
00:06:27,520 --> 00:06:29,600
now there's nothing really to wait for
177
00:06:29,600 --> 00:06:31,440
it here everything runs
178
00:06:31,440 --> 00:06:33,840
immediately okay so that is just an
179
00:06:33,840 --> 00:06:35,199
example of callback
180
00:06:35,199 --> 00:06:38,160
running synchronously um hopefully this
181
00:06:38,160 --> 00:06:39,759
lesson has been really useful
182
00:06:39,759 --> 00:06:42,080
in learning a little bit about what a
183
00:06:42,080 --> 00:06:43,039
callback
184
00:06:43,039 --> 00:06:46,080
function is and how you would use it
185
00:06:46,080 --> 00:06:49,440
in asynchronous javascript
186
00:06:49,440 --> 00:06:52,639
okay so just to recap this
187
00:06:52,639 --> 00:06:55,120
is a callback we are passing a function
188
00:06:55,120 --> 00:06:56,400
into another function
189
00:06:56,400 --> 00:06:59,440
only to be called later when it releases
190
00:06:59,440 --> 00:07:01,840
all of its function energy thanks to the
191
00:07:01,840 --> 00:07:03,280
parenthesis
192
00:07:03,280 --> 00:07:06,080
this is also a callback function so you
193
00:07:06,080 --> 00:07:07,360
can write it as
194
00:07:07,360 --> 00:07:10,319
this where the function is separate and
195
00:07:10,319 --> 00:07:11,680
you're just passing through the
196
00:07:11,680 --> 00:07:12,880
reference for it
197
00:07:12,880 --> 00:07:15,280
or you can write it like this okay and
198
00:07:15,280 --> 00:07:17,199
this is of course the parenthesis
199
00:07:17,199 --> 00:07:20,080
parenthesis uh arrow function and the
200
00:07:20,080 --> 00:07:21,360
cardio braces
201
00:07:21,360 --> 00:07:25,120
it is totally up to you next up now that
202
00:07:25,120 --> 00:07:26,479
we have looked at quarterbacks
203
00:07:26,479 --> 00:07:28,960
especially in the context of the xml
204
00:07:28,960 --> 00:07:31,360
http requests api
205
00:07:31,360 --> 00:07:34,639
let's move on to promises i will see you
206
00:07:34,639 --> 00:07:39,039
in the next video13960
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.