Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,080 --> 00:00:02,639
hello my internet friends and welcome to
2
00:00:02,639 --> 00:00:04,240
this video which we're gonna be learning
3
00:00:04,240 --> 00:00:04,799
about
4
00:00:04,799 --> 00:00:07,279
asynchronous javascript by understanding
5
00:00:07,279 --> 00:00:08,559
what synchronous
6
00:00:08,559 --> 00:00:12,000
or sync javascript is first
7
00:00:12,000 --> 00:00:13,920
this video is actually part of a little
8
00:00:13,920 --> 00:00:16,240
asynchronous javascript mini series i am
9
00:00:16,240 --> 00:00:18,080
doing so if you do enjoy this video
10
00:00:18,080 --> 00:00:19,520
please do go on to
11
00:00:19,520 --> 00:00:21,199
watch the others to really get a
12
00:00:21,199 --> 00:00:23,600
well-rounded uh learning experience of
13
00:00:23,600 --> 00:00:26,000
what asynchronous javascript is all
14
00:00:26,000 --> 00:00:28,560
about but before we get going a quick
15
00:00:28,560 --> 00:00:30,480
word from our video sponsors which is
16
00:00:30,480 --> 00:00:32,719
the reason that this video is here for
17
00:00:32,719 --> 00:00:33,280
you
18
00:00:33,280 --> 00:00:36,320
today hashnote is one of the easiest
19
00:00:36,320 --> 00:00:38,320
ways to start a developer blog that will
20
00:00:38,320 --> 00:00:38,640
get
21
00:00:38,640 --> 00:00:41,040
seen globally if you are looking to get
22
00:00:41,040 --> 00:00:43,200
noticed by the developer community
23
00:00:43,200 --> 00:00:45,760
hashnode will help distribute your posts
24
00:00:45,760 --> 00:00:47,360
on their platform
25
00:00:47,360 --> 00:00:48,960
it's a blogging site built for
26
00:00:48,960 --> 00:00:51,360
developers by developers
27
00:00:51,360 --> 00:00:53,680
and not only that you can move it onto
28
00:00:53,680 --> 00:00:55,039
your personal domain
29
00:00:55,039 --> 00:00:57,600
super easy too just check out these
30
00:00:57,600 --> 00:00:58,320
wonderful
31
00:00:58,320 --> 00:01:01,120
reviews please do have a go at signing
32
00:01:01,120 --> 00:01:01,440
up
33
00:01:01,440 --> 00:01:03,440
and if you do please use the link in my
34
00:01:03,440 --> 00:01:05,519
description to let them know this ad
35
00:01:05,519 --> 00:01:08,640
has worked now if you have ever
36
00:01:08,640 --> 00:01:11,119
encountered a website that relies on a
37
00:01:11,119 --> 00:01:12,159
database or an
38
00:01:12,159 --> 00:01:15,200
api or some other third-party service
39
00:01:15,200 --> 00:01:16,080
that can take
40
00:01:16,080 --> 00:01:18,560
time retrieving our data it is likely
41
00:01:18,560 --> 00:01:20,320
that you have already come across
42
00:01:20,320 --> 00:01:23,600
a use case for asynchronous javascript
43
00:01:23,600 --> 00:01:25,040
to allow us to understand what
44
00:01:25,040 --> 00:01:26,720
asynchronous javascript
45
00:01:26,720 --> 00:01:29,360
is let's start off by making sure we
46
00:01:29,360 --> 00:01:30,400
understand what
47
00:01:30,400 --> 00:01:34,479
synchronous javascript is too
48
00:01:34,479 --> 00:01:37,040
so if you have covered the fundamentals
49
00:01:37,040 --> 00:01:39,439
of javascript you would have unknowingly
50
00:01:39,439 --> 00:01:41,680
been coding the majority of your lessons
51
00:01:41,680 --> 00:01:46,000
synchronously let's look at some code
52
00:01:46,000 --> 00:01:48,960
here we have three functions now if i
53
00:01:48,960 --> 00:01:50,640
call the functions like
54
00:01:50,640 --> 00:01:54,560
this so all three of them here
55
00:01:54,560 --> 00:01:57,040
what do you expect will happen in the
56
00:01:57,040 --> 00:01:58,320
console
57
00:01:58,320 --> 00:02:00,799
we will get these strings i'm the first
58
00:02:00,799 --> 00:02:01,759
action
59
00:02:01,759 --> 00:02:04,240
i'm the second action and i'm the third
60
00:02:04,240 --> 00:02:04,880
action
61
00:02:04,880 --> 00:02:08,160
show up in exactly that order
62
00:02:08,160 --> 00:02:10,879
this is because javascript runs from top
63
00:02:10,879 --> 00:02:11,760
to bottom
64
00:02:11,760 --> 00:02:14,640
or in other words sequentially the lines
65
00:02:14,640 --> 00:02:14,959
are
66
00:02:14,959 --> 00:02:18,400
executed one after the other
67
00:02:18,400 --> 00:02:21,760
now let's look at another example
68
00:02:21,760 --> 00:02:24,560
so here we have an example in which i am
69
00:02:24,560 --> 00:02:25,280
grabbing
70
00:02:25,280 --> 00:02:28,879
a button from our html and then
71
00:02:28,879 --> 00:02:31,200
grabbing that button again and using the
72
00:02:31,200 --> 00:02:33,920
add event listener javascript method
73
00:02:33,920 --> 00:02:36,879
and passing through the event of click
74
00:02:36,879 --> 00:02:37,519
followed
75
00:02:37,519 --> 00:02:40,560
by some code then we have the console
76
00:02:40,560 --> 00:02:41,920
log of count
77
00:02:41,920 --> 00:02:44,800
followed by document create element in
78
00:02:44,800 --> 00:02:47,200
which i create an h1 tag
79
00:02:47,200 --> 00:02:50,560
and assign it to the const text
80
00:02:50,560 --> 00:02:52,959
next i'm grabbing the text and giving it
81
00:02:52,959 --> 00:02:53,920
a string
82
00:02:53,920 --> 00:02:56,239
which has nothing to do with the count
83
00:02:56,239 --> 00:02:56,959
and then
84
00:02:56,959 --> 00:03:00,080
sticking in our html
85
00:03:00,080 --> 00:03:03,840
so if i click this button in our browser
86
00:03:03,840 --> 00:03:06,080
when do you expect the text to show up
87
00:03:06,080 --> 00:03:08,720
in the browser
88
00:03:08,720 --> 00:03:12,000
let's have a look and see you will
89
00:03:12,000 --> 00:03:14,159
notice that the text only shows up
90
00:03:14,159 --> 00:03:18,000
after the entire loop has completed
91
00:03:18,000 --> 00:03:20,640
this will take a bit of time not a lot
92
00:03:20,640 --> 00:03:22,560
in this example but definitely more than
93
00:03:22,560 --> 00:03:24,080
say if we were to do
94
00:03:24,080 --> 00:03:28,319
one loop like this
95
00:03:29,519 --> 00:03:32,879
but why does this time delay happen well
96
00:03:32,879 --> 00:03:35,360
while each operation is being processed
97
00:03:35,360 --> 00:03:37,040
nothing else can happen
98
00:03:37,040 --> 00:03:40,000
rendering is paused this is because
99
00:03:40,000 --> 00:03:41,200
javascript is
100
00:03:41,200 --> 00:03:43,840
single threaded only one thing can
101
00:03:43,840 --> 00:03:45,200
happen at a time
102
00:03:45,200 --> 00:03:48,319
on a single main thread and everything
103
00:03:48,319 --> 00:03:48,959
else is
104
00:03:48,959 --> 00:03:52,799
blocked until an operation completes
105
00:03:52,799 --> 00:03:54,319
before we move on it's actually
106
00:03:54,319 --> 00:03:56,239
important to tell you that today
107
00:03:56,239 --> 00:03:59,040
many computers have multiple cores so
108
00:03:59,040 --> 00:04:01,599
can do multiple things at once
109
00:04:01,599 --> 00:04:03,599
programming languages that can support
110
00:04:03,599 --> 00:04:05,120
multiple threads can use
111
00:04:05,120 --> 00:04:07,599
multiple cores to complete multiple
112
00:04:07,599 --> 00:04:08,239
tasks
113
00:04:08,239 --> 00:04:11,680
simultaneously great now that we have
114
00:04:11,680 --> 00:04:12,239
covered
115
00:04:12,239 --> 00:04:14,720
synchronous code let's get into
116
00:04:14,720 --> 00:04:17,040
asynchronous code
117
00:04:17,040 --> 00:04:18,720
now for the reason we spoke about
118
00:04:18,720 --> 00:04:20,400
earlier to do with blocking
119
00:04:20,400 --> 00:04:22,720
many web api features now use
120
00:04:22,720 --> 00:04:24,479
asynchronous code
121
00:04:24,479 --> 00:04:26,800
this is especially true for those that
122
00:04:26,800 --> 00:04:27,520
access
123
00:04:27,520 --> 00:04:29,440
or fetch something from something
124
00:04:29,440 --> 00:04:30,560
external
125
00:04:30,560 --> 00:04:32,320
this can be fetching a file from the
126
00:04:32,320 --> 00:04:35,360
network or getting data from a database
127
00:04:35,360 --> 00:04:37,520
or accessing a video stream from a
128
00:04:37,520 --> 00:04:39,040
webcam and so
129
00:04:39,040 --> 00:04:42,560
so much more now this can sometimes be
130
00:04:42,560 --> 00:04:46,800
problematic let's have a look at why
131
00:04:46,800 --> 00:04:48,960
here is some code that's been written in
132
00:04:48,960 --> 00:04:50,080
an attempt to fetch
133
00:04:50,080 --> 00:04:52,479
an external image so we can use it in
134
00:04:52,479 --> 00:04:54,080
our file
135
00:04:54,080 --> 00:04:57,360
when you fetch an image from a server
136
00:04:57,360 --> 00:05:00,080
it is unlikely that it will be returned
137
00:05:00,080 --> 00:05:01,360
immediately
138
00:05:01,360 --> 00:05:03,199
this is because the image can take a
139
00:05:03,199 --> 00:05:05,280
long time to come to us
140
00:05:05,280 --> 00:05:07,600
this means that when it comes to running
141
00:05:07,600 --> 00:05:09,120
the second line
142
00:05:09,120 --> 00:05:12,639
here it could throw an error
143
00:05:12,639 --> 00:05:14,880
because the response is not yet
144
00:05:14,880 --> 00:05:16,000
available
145
00:05:16,000 --> 00:05:17,680
so in other words you have no image
146
00:05:17,680 --> 00:05:20,000
that's come back for you to work with
147
00:05:20,000 --> 00:05:22,639
so think of it like this here we have
148
00:05:22,639 --> 00:05:23,199
some
149
00:05:23,199 --> 00:05:25,759
code here we have the code that is
150
00:05:25,759 --> 00:05:28,160
responsible for fetching the image
151
00:05:28,160 --> 00:05:30,080
here we have some code that displays the
152
00:05:30,080 --> 00:05:32,080
image in our browser
153
00:05:32,080 --> 00:05:36,560
all in one thread now if we run our code
154
00:05:36,560 --> 00:05:37,440
we will run it
155
00:05:37,440 --> 00:05:39,919
sequentially so i'm bringing out all the
156
00:05:39,919 --> 00:05:41,280
buzzwords here
157
00:05:41,280 --> 00:05:43,120
this means that this code will get run
158
00:05:43,120 --> 00:05:45,280
from top to bottom
159
00:05:45,280 --> 00:05:48,080
okay so this code will get run this code
160
00:05:48,080 --> 00:05:49,440
will get run but uh-oh
161
00:05:49,440 --> 00:05:50,880
it's taking a long time to get a
162
00:05:50,880 --> 00:05:52,720
response
163
00:05:52,720 --> 00:05:55,440
in the meantime the third cone gets run
164
00:05:55,440 --> 00:05:57,600
and we have no image to show
165
00:05:57,600 --> 00:06:00,560
and we get errors okay so we are running
166
00:06:00,560 --> 00:06:02,080
it but there's no image
167
00:06:02,080 --> 00:06:04,280
the part here is being run
168
00:06:04,280 --> 00:06:05,440
asynchronously
169
00:06:05,440 --> 00:06:07,360
but at the same time the rest of the
170
00:06:07,360 --> 00:06:09,600
code is living its life as usual
171
00:06:09,600 --> 00:06:12,400
and moving on without the response that
172
00:06:12,400 --> 00:06:14,720
is not the behavior we want
173
00:06:14,720 --> 00:06:16,960
we need to get the images to show right
174
00:06:16,960 --> 00:06:18,400
but how
175
00:06:18,400 --> 00:06:21,440
for this we can use either an old style
176
00:06:21,440 --> 00:06:24,560
callback or a new style promise
177
00:06:24,560 --> 00:06:27,759
we will go into these in the next video
178
00:06:27,759 --> 00:06:32,880
in this mini-series i'll see you soon12080
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.