Fireship · @fireship
The Async Await Episode I Promised
Uploaded October 11, 2018 · Length 12:04
Summary
javascript async await explained with examples and code demos
- javascript async await
- event loop
- promises
- callbacks
- async programming
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
[Music]
- 0:06
javascript is a single-threaded
- 0:08
programming language yet everything we
- 0:10
do on the web tends to be blocking or
- 0:11
time-consuming which means that
- 0:13
asynchronous programming is an essential
- 0:15
skill for any JavaScript developer
- 0:17
today's video will focus primarily on
- 0:19
the amazing syntactic sugar provided by
- 0:21
async await but before we can get there
- 0:23
we really need to understand things from
- 0:24
the ground up starting with the browser
- 0:26
or nodejs event loop callbacks promises
- 0:29
and then finally async await and we're
- 0:31
going to do this without making a single
- 0:33
joke about a sink or a wave if you're
- 0:35
new here like and subscribe and we'll be
- 0:37
giving away another one of a kind
- 0:38
t-shirt with this video all you have to
- 0:39
do is leave a comment below and we'll
- 0:41
pick a random winner next week in order
- 0:43
to understand anything async you really
- 0:45
need to first understand the event loop
- 0:47
this is ace I hope that clears up any
- 0:50
questions you have I'm going to give you
- 0:52
the general overview but I highly
- 0:54
recommend you watch this talk from Jake
- 0:55
Archibald which is the best explanation
- 0:57
of the event loop that I've ever seen
- 0:59
then after you're done watching that
- 1:00
talk you should check out Stephen
- 1:02
fluence demos with angular Channel which
- 1:04
just released a video about callbacks
- 1:05
which are one of the things that happen
- 1:07
within the event loop so what is the
- 1:09
event loop and what does it have to do
- 1:10
with async away both the browser and
- 1:13
nodejs are always running a single
- 1:15
threaded event loop to run your code on
- 1:17
the first go around it will run all of
- 1:19
your synchronous code but it might also
- 1:21
queue up asynchronous events to be
- 1:22
called back later you say here's a
- 1:24
function that I need to run but first I
- 1:25
need to go get some data from the
- 1:27
network the event loop says okay I'll
- 1:29
keep doing my thing while you do your
- 1:30
thing in a separate thread pool then at
- 1:32
some point in the future get data will
- 1:34
finish and let the event loop know that
- 1:36
it's ready to be called back now this is
- 1:38
where things get interesting if it's a
- 1:39
macro task like a set timeout or set
- 1:42
interval it will be executed on the next
- 1:44
event loop but if it's a micro task like
- 1:46
a fulfilled promise then it will be
- 1:48
called back before the start of the next
- 1:50
event loop let's look at the
- 1:51
implications of this by writing some
- 1:53
actual code first we will write a
- 1:55
console log which is synchronous then
- 1:57
we'll throw in a set timeout but give it
- 1:58
a time delay of zero milliseconds then


