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:02,159
hello everyone in this video i'm going
2
00:00:02,159 --> 00:00:03,679
to deep dive into what
3
00:00:03,679 --> 00:00:06,799
promises are and how to use them using
4
00:00:06,799 --> 00:00:09,280
javascript this video is part of a
5
00:00:09,280 --> 00:00:11,280
little mini series that i am doing on
6
00:00:11,280 --> 00:00:13,440
asynchronous javascript starting with
7
00:00:13,440 --> 00:00:14,240
ajax
8
00:00:14,240 --> 00:00:16,880
moving on to async versus synchronous
9
00:00:16,880 --> 00:00:17,920
javascript
10
00:00:17,920 --> 00:00:20,480
having a look at callbacks and now
11
00:00:20,480 --> 00:00:21,439
moving on to
12
00:00:21,439 --> 00:00:25,119
promises so what are promises
13
00:00:25,119 --> 00:00:26,960
as we know there are two types of
14
00:00:26,960 --> 00:00:28,880
asynchronous code styles you will come
15
00:00:28,880 --> 00:00:30,720
across in javascript
16
00:00:30,720 --> 00:00:33,120
those are the also callbacks and the
17
00:00:33,120 --> 00:00:34,160
newest style
18
00:00:34,160 --> 00:00:37,040
promises you will see promises used in
19
00:00:37,040 --> 00:00:38,879
modern web apis
20
00:00:38,879 --> 00:00:41,920
a great example of this is the fetch
21
00:00:41,920 --> 00:00:44,719
api it is basically the modern
22
00:00:44,719 --> 00:00:45,360
equivalent
23
00:00:45,360 --> 00:00:48,480
of the xml http request you would have
24
00:00:48,480 --> 00:00:50,719
seen if you watched my previous
25
00:00:50,719 --> 00:00:54,239
video let's have a look at it in
26
00:00:54,239 --> 00:00:58,000
code so here we have the fetch api in
27
00:00:58,000 --> 00:00:58,480
action
28
00:00:58,480 --> 00:01:00,559
all i'm doing it is using it in order to
29
00:01:00,559 --> 00:01:02,640
get all the countries from this
30
00:01:02,640 --> 00:01:06,000
rest api right here it is called
31
00:01:06,000 --> 00:01:08,159
rest countries and i use it quite a lot
32
00:01:08,159 --> 00:01:09,280
in these types of
33
00:01:09,280 --> 00:01:13,439
tutorials i can also write it like this
34
00:01:13,439 --> 00:01:15,520
it's the same thing and more widely
35
00:01:15,520 --> 00:01:17,600
adopted so i'm just switching these
36
00:01:17,600 --> 00:01:20,640
out for our functions like
37
00:01:20,640 --> 00:01:24,320
so in this code fetch takes
38
00:01:24,320 --> 00:01:27,520
one parameter which is a url if we look
39
00:01:27,520 --> 00:01:29,759
at this url so i'm just going to take it
40
00:01:29,759 --> 00:01:32,320
and copy it into my browser
41
00:01:32,320 --> 00:01:34,640
it will show you some json which i
42
00:01:34,640 --> 00:01:36,640
prettified using the json
43
00:01:36,640 --> 00:01:39,920
prettifier chrome extension tool so we
44
00:01:39,920 --> 00:01:42,000
can read it a little bit better
45
00:01:42,000 --> 00:01:44,479
this jason is essentially a bunch of
46
00:01:44,479 --> 00:01:45,360
objects that
47
00:01:45,360 --> 00:01:48,799
are countries they have the country
48
00:01:48,799 --> 00:01:51,759
name country flag and a bunch of other
49
00:01:51,759 --> 00:01:52,320
stuff
50
00:01:52,320 --> 00:01:56,320
to our disposal fetching this url will
51
00:01:56,320 --> 00:01:57,520
return a
52
00:01:57,520 --> 00:02:01,200
promise so if i simply comment this all
53
00:02:01,200 --> 00:02:04,799
out and write this
54
00:02:04,799 --> 00:02:07,040
so literally just use the fetch and pass
55
00:02:07,040 --> 00:02:08,399
through the url
56
00:02:08,399 --> 00:02:11,120
and console log out the response you
57
00:02:11,120 --> 00:02:11,760
will see a
58
00:02:11,760 --> 00:02:14,800
promise is returned
59
00:02:14,800 --> 00:02:17,120
the promise is an object representing
60
00:02:17,120 --> 00:02:19,599
the completion or failure of the async
61
00:02:19,599 --> 00:02:20,879
operation
62
00:02:20,879 --> 00:02:24,239
it represents an intermediate state
63
00:02:24,239 --> 00:02:26,239
it is essentially the browser's way of
64
00:02:26,239 --> 00:02:27,360
saying hey
65
00:02:27,360 --> 00:02:29,920
i promise i will get back to you as soon
66
00:02:29,920 --> 00:02:32,319
as possible
67
00:02:32,319 --> 00:02:35,120
so essentially if you ever see this you
68
00:02:35,120 --> 00:02:36,480
know you are dealing with an
69
00:02:36,480 --> 00:02:40,160
async operation this is why we need
70
00:02:40,160 --> 00:02:41,360
other code blocks
71
00:02:41,360 --> 00:02:45,360
chained onto the end of fetch
72
00:02:45,360 --> 00:02:49,040
first we have a then block as you would
73
00:02:49,040 --> 00:02:50,000
probably have noticed by
74
00:02:50,000 --> 00:02:52,879
now it contains a callback function that
75
00:02:52,879 --> 00:02:55,360
will run if the previous operation
76
00:02:55,360 --> 00:02:58,319
is successful it will then receive the
77
00:02:58,319 --> 00:03:00,720
result of the previous operation
78
00:03:00,720 --> 00:03:03,920
in this case we want the responses jason
79
00:03:03,920 --> 00:03:04,560
and once
80
00:03:04,560 --> 00:03:07,680
that is complete we move on to the next
81
00:03:07,680 --> 00:03:10,480
block of code this also actually
82
00:03:10,480 --> 00:03:12,239
contains a callback
83
00:03:12,239 --> 00:03:15,680
function i am getting the return of the
84
00:03:15,680 --> 00:03:18,239
previous successful operation and i'm
85
00:03:18,239 --> 00:03:19,200
assigning it
86
00:03:19,200 --> 00:03:22,159
to the variable countries and console
87
00:03:22,159 --> 00:03:24,080
logging it so we can see it in our
88
00:03:24,080 --> 00:03:27,599
console okay so a lot of callbacks going
89
00:03:27,599 --> 00:03:27,920
on
90
00:03:27,920 --> 00:03:30,400
in there if you watch the previous video
91
00:03:30,400 --> 00:03:33,200
on callbacks this will all be making a
92
00:03:33,200 --> 00:03:34,239
lot more
93
00:03:34,239 --> 00:03:37,840
sense each then block is essentially
94
00:03:37,840 --> 00:03:39,200
returning another
95
00:03:39,200 --> 00:03:41,440
promise so essentially multiple
96
00:03:41,440 --> 00:03:43,840
asynchronous operations are being made
97
00:03:43,840 --> 00:03:46,799
that are being made to run in order one
98
00:03:46,799 --> 00:03:47,360
after
99
00:03:47,360 --> 00:03:50,080
the other so in other words all the
100
00:03:50,080 --> 00:03:52,319
async operations or promises
101
00:03:52,319 --> 00:03:55,519
are being put into an event queue
102
00:03:55,519 --> 00:03:58,640
this event queue will run after the main
103
00:03:58,640 --> 00:04:01,680
thread has finished processing so that
104
00:04:01,680 --> 00:04:02,560
they do not
105
00:04:02,560 --> 00:04:05,599
block the other javascript code from
106
00:04:05,599 --> 00:04:09,040
running and finally we have the catch
107
00:04:09,040 --> 00:04:10,239
block
108
00:04:10,239 --> 00:04:12,560
this block is put at the end and once in
109
00:04:12,560 --> 00:04:13,360
any of the
110
00:04:13,360 --> 00:04:16,479
then blocks fail it works in a similar
111
00:04:16,479 --> 00:04:17,600
way to the synchronous
112
00:04:17,600 --> 00:04:20,720
try catch blocks but that is something
113
00:04:20,720 --> 00:04:22,240
that we will leave for now and learn
114
00:04:22,240 --> 00:04:22,800
about
115
00:04:22,800 --> 00:04:27,280
more of this in the async 08 video
116
00:04:27,280 --> 00:04:30,320
now time for a little pop quiz
117
00:04:30,320 --> 00:04:32,479
what if i display the first flag on the
118
00:04:32,479 --> 00:04:34,479
first country in our browser
119
00:04:34,479 --> 00:04:37,840
so i add this code
120
00:04:37,840 --> 00:04:40,560
i am creating an image and then
121
00:04:40,560 --> 00:04:41,440
appending it
122
00:04:41,440 --> 00:04:44,639
to the body of my document next i'm
123
00:04:44,639 --> 00:04:45,680
gonna just go
124
00:04:45,680 --> 00:04:47,680
into the json let's return and get the
125
00:04:47,680 --> 00:04:49,280
first object and get its
126
00:04:49,280 --> 00:04:51,600
flag and assign this to the image that
127
00:04:51,600 --> 00:04:52,639
we just made
128
00:04:52,639 --> 00:04:55,680
source i'm also gonna add console.log
129
00:04:55,680 --> 00:04:56,000
flag
130
00:04:56,000 --> 00:04:57,680
added so we can see what is going on
131
00:04:57,680 --> 00:04:59,919
under the hood
132
00:04:59,919 --> 00:05:01,840
let's do the same for console.log done
133
00:05:01,840 --> 00:05:02,960
so after
134
00:05:02,960 --> 00:05:05,039
everything has run and up here
135
00:05:05,039 --> 00:05:08,160
console.log let's go
136
00:05:09,520 --> 00:05:13,120
now if i run this code what order would
137
00:05:13,120 --> 00:05:15,199
you expect the console logs to appear in
138
00:05:15,199 --> 00:05:17,039
the console
139
00:05:17,039 --> 00:05:20,560
have a think about it you would see
140
00:05:20,560 --> 00:05:24,479
let's go done flag added
141
00:05:24,479 --> 00:05:26,880
the browser will begin executing the
142
00:05:26,880 --> 00:05:28,800
code sequentially
143
00:05:28,800 --> 00:05:31,600
the first block of code is the
144
00:05:31,600 --> 00:05:33,199
console.log with
145
00:05:33,199 --> 00:05:36,720
let's go so that will be executed
146
00:05:36,720 --> 00:05:40,240
next we will move on to the fetch block
147
00:05:40,240 --> 00:05:43,840
so this one right here this however will
148
00:05:43,840 --> 00:05:44,800
be executed
149
00:05:44,800 --> 00:05:47,440
asynchronously without blocking anything
150
00:05:47,440 --> 00:05:48,320
else
151
00:05:48,320 --> 00:05:51,440
so we carry on we then execute the
152
00:05:51,440 --> 00:05:54,560
done console log as that is next
153
00:05:54,560 --> 00:05:57,680
note how all the other code right
154
00:05:57,680 --> 00:06:00,880
here is chained to the batch
155
00:06:00,880 --> 00:06:04,400
so it's part of the same code block as
156
00:06:04,400 --> 00:06:07,680
the fetch so once the fetch block has
157
00:06:07,680 --> 00:06:08,880
finished running
158
00:06:08,880 --> 00:06:11,280
and delivered the result we will then
159
00:06:11,280 --> 00:06:13,280
see the flag and of course
160
00:06:13,280 --> 00:06:16,880
also console.log flag
161
00:06:16,880 --> 00:06:18,880
so hopefully this will now make sense if
162
00:06:18,880 --> 00:06:20,000
i wanted to actually
163
00:06:20,000 --> 00:06:22,400
get the flag source down here so when we
164
00:06:22,400 --> 00:06:23,440
console log
165
00:06:23,440 --> 00:06:25,120
done and you should hopefully now
166
00:06:25,120 --> 00:06:27,360
understand why if we run this
167
00:06:27,360 --> 00:06:29,440
we would not get the flag source we will
168
00:06:29,440 --> 00:06:31,600
get an error because everything else is
169
00:06:31,600 --> 00:06:33,440
still being run we are still fetching
170
00:06:33,440 --> 00:06:34,080
all that
171
00:06:34,080 --> 00:06:36,639
data if this confuses you let's have a
172
00:06:36,639 --> 00:06:37,919
look at something similar
173
00:06:37,919 --> 00:06:41,440
but smaller so i'm just gonna
174
00:06:41,440 --> 00:06:44,560
get rid of this and
175
00:06:44,560 --> 00:06:48,479
paste so just like we pass
176
00:06:48,479 --> 00:06:50,960
a callback function into fetch we are
177
00:06:50,960 --> 00:06:51,680
passing a
178
00:06:51,680 --> 00:06:53,919
callback function into the add event
179
00:06:53,919 --> 00:06:55,919
listener javascript method
180
00:06:55,919 --> 00:06:58,319
if we run this code the first console
181
00:06:58,319 --> 00:06:59,039
log will get
182
00:06:59,039 --> 00:07:02,319
executed as no one has yet clicked the
183
00:07:02,319 --> 00:07:04,319
button or in other words the ad event
184
00:07:04,319 --> 00:07:06,400
listener has not listened to a click
185
00:07:06,400 --> 00:07:10,000
happening on the element of button
186
00:07:10,000 --> 00:07:12,720
this is blocked from running until that
187
00:07:12,720 --> 00:07:14,160
happens
188
00:07:14,160 --> 00:07:16,240
so it works just like our previous
189
00:07:16,240 --> 00:07:18,800
example except that in the previous case
190
00:07:18,800 --> 00:07:20,080
the console
191
00:07:20,080 --> 00:07:22,800
log is blocked on the promised chain
192
00:07:22,800 --> 00:07:24,319
fetching the url
193
00:07:24,319 --> 00:07:27,599
rather than the click got it
194
00:07:27,599 --> 00:07:30,800
good before we move on to the
195
00:07:30,800 --> 00:07:33,280
next section let's actually recap what
196
00:07:33,280 --> 00:07:35,280
we have learnt by having a look at
197
00:07:35,280 --> 00:07:38,479
promises versus callbacks
198
00:07:38,479 --> 00:07:40,080
promises are kind of similar to
199
00:07:40,080 --> 00:07:41,520
callbacks in that they
200
00:07:41,520 --> 00:07:43,919
are a returned object which you attach
201
00:07:43,919 --> 00:07:44,800
callback
202
00:07:44,800 --> 00:07:47,120
functions rather than having to pass
203
00:07:47,120 --> 00:07:49,280
callbacks into a function
204
00:07:49,280 --> 00:07:52,720
however they have a bunch of extra press
205
00:07:52,720 --> 00:07:54,879
promises are specifically made for
206
00:07:54,879 --> 00:07:57,199
handling async operations
207
00:07:57,199 --> 00:07:59,199
you can also chain multiple async
208
00:07:59,199 --> 00:08:01,680
operations together using the then
209
00:08:01,680 --> 00:08:04,960
operators doing something like this with
210
00:08:04,960 --> 00:08:05,759
callbacks
211
00:08:05,759 --> 00:08:09,039
is much harder and messier promised
212
00:08:09,039 --> 00:08:10,319
callbacks are always
213
00:08:10,319 --> 00:08:12,479
called in the strict order they are
214
00:08:12,479 --> 00:08:13,919
placed in an event
215
00:08:13,919 --> 00:08:17,120
queue error handling is also much better
216
00:08:17,120 --> 00:08:20,400
thanks to the catch block promises
217
00:08:20,400 --> 00:08:23,440
also avoid inversion of control unlike
218
00:08:23,440 --> 00:08:26,000
callbacks which lose full control of how
219
00:08:26,000 --> 00:08:28,080
functions will be executed when passing
220
00:08:28,080 --> 00:08:30,160
a callback to a third-party
221
00:08:30,160 --> 00:08:33,760
library okay great with the promises
222
00:08:33,760 --> 00:08:34,399
covered
223
00:08:34,399 --> 00:08:37,120
i think it's time to move on please feel
224
00:08:37,120 --> 00:08:39,039
free to go over this section again
225
00:08:39,039 --> 00:08:41,279
as promises are known to be confusing
226
00:08:41,279 --> 00:08:43,519
and take a lot of practice to get
227
00:08:43,519 --> 00:08:46,880
used to remember praxis always makes
228
00:08:46,880 --> 00:08:47,519
perfect
229
00:08:47,519 --> 00:08:50,040
and that cannot be more true with this
230
00:08:50,040 --> 00:08:53,040
lesson15656
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.