Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Asynchronous Programming with JavaScript!
You have completed Asynchronous Programming with JavaScript!
Preview
To better understand what asynchronous code is and why it's necessary, you should also understand what synchronous code is.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Tasks in real life are often
completed in an asynchronous way.
0:00
We really can't do two or
more things exactly at the same time, but
0:04
we can be efficient in how
we switch tasks and context.
0:08
For example, we don't always wait for
0:12
one task to be completely finished
before moving on to the next.
0:14
If you're doing chores for instance,
you might make the bed and
0:18
vacuum while waiting for
the dryer to finish.
0:21
Being able to get other
work done in the meantime
0:24
is the basis of asynchronous programming.
0:27
To better understand what asynchronous
code is and why it's necessary,
0:30
you should also understand
what synchronous code is.
0:33
Let's say that you're making breakfast,
it consists of three main tasks.
0:37
Make two hard boiled eggs,
make toast, and make coffee.
0:41
First, let's have a look at how you
might approach preparing breakfast
0:45
synchronously, or
by completing one task at a time.
0:48
You begin by boiling the water for
the eggs.
0:52
Once the water starts to boil,
you carefully drop in one egg.
0:54
You wait for that egg to finish cooking,
then you peel it.
0:59
Next, you drop the second egg into
the water and follow the same steps.
1:02
Once you've completed your egg cooking
duties, you move on to making toast.
1:06
You get a slice of bread,
place it in the toaster, and wait for
1:09
the toasting cycle to complete.
1:12
Finally, you turn on the coffee maker,
wait for
1:14
it to heat up, then start making
coffee by going through all
1:16
the steps necessary to brew your
favorite coffee until it's done.
1:19
This might sound like an inefficient
way to make breakfast.
1:23
This approach can take a considerable
amount of time, leaving you with
1:26
no option but to wait for one task to
complete before moving on to the next.
1:30
Now, let's look at how you might
make breakfast asynchronously,
1:34
like you probably would in real life.
1:38
You begin to boil the water,
then you start to get
1:40
other work done in the meantime
while the water gets to a boil.
1:43
For instance, you might get your bread,
place it in the toaster, and
1:46
turn on the coffee maker
to let it heat up.
1:49
Once the water starts to boil,
you drop in both eggs,
1:52
maybe you even set an egg timer to let you
know when they're cooked to your liking.
1:54
While the eggs cook,
you might start to make coffee and
1:58
maybe take on other
tasks like feed the cat.
2:01
After the eggs are cooked,
2:03
you begin toasting the bread
while you peel the eggs.
2:05
Finally, you place your breakfast on
a plate and pour yourself a cup coffee and
2:07
enjoy your breakfast.
2:11
Making breakfast seems more
efficient this way, doesn't it?
2:13
In a synchronous programming model,
2:17
two or more things cannot
happen at the same time.
2:18
Only one thing is able to run
to completion at any given time.
2:21
That's like waiting for
the water to boil, and
2:25
each egg to cook one at a time before
you can even turn on the coffee machine.
2:27
On the other hand, in an asynchronous
model a block of code can start to run,
2:32
and even if that block of code
is expected to finish what it
2:37
needs to do at some point later,
other code is able to run in the meantime.
2:40
When that chunk of code is finally
ready to complete what it needs to do,
2:45
the program or the JavaScript engine for
example, gets back to it and
2:48
executes that task.
2:52
That's like making coffee and
toast while your eggs cook and
2:53
getting back to the eggs
when the timer goes off.
2:56
Not letting one task block
the completion of another task
2:59
is how JavaScript can seemingly
do more than one thing at a time.
3:02
As you'll learn,
3:06
this asynchronous behavior is necessary
because the kind of tasks JavaScript is
3:07
asked are often time consuming,
like making network requests for example.
3:12
Now that you have a better understanding
of the differences between synchronous and
3:17
asynchronous code, in the next video,
3:20
you'll look at real life
examples of both in the browser.
3:22
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up