Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
A Promise
represents the eventual completion of an asynchronous operation.
Promises provide a straightforward alternative for composing and managing asynchronous code, without having to write too many callbacks, or spend extra time figuring out what the program should do.
[MUSIC]
0:00
Promises in JavaScript offer
a more elegant and human readable
0:04
way to manage asynchronous code without
having to write too many callbacks, or
0:07
spend extra time figuring out
what the program should do.
0:12
Promises also give you more
control over your asynch programs.
0:16
What is a promise?
0:20
Well a promise represents the eventual
completion of a asynchronous operation.
0:21
In other words,
0:26
a value that may not be available yet,
like data requested from a server.
0:27
So let's spend a little bit of
time unpacking what all this means
0:31
by first looking at the differences
between callbacks and promises.
0:34
Let's say that,
0:39
instead of making breakfast yourself,
you're in the mood to order breakfast.
0:39
There are two ways you
might order breakfast.
0:43
You can order by phone and
have it delivered, or
0:45
you can order at the restaurant and
pick it up when ready.
0:48
In the first scenario,
you call your favorite breakfast spot and
0:51
place your order, providing them
your phone number and address.
0:55
The order taker says okay, thanks.
0:58
I'll put in your order now and
1:00
call you back to let you know when
the order is ready and on its way.
1:02
And that's it.
1:04
At this point, you can go on and
1:05
do other things while breakfast
gets there, that's convenient.
1:07
But you're also left waiting for
a call back from the restaurant.
1:10
You really don't have control over
when they're going to call you.
1:14
You received nothing in exchange.
1:17
For example,
a ticket number to confirm your order.
1:19
You just wait for them to call and
1:21
deliver your food at around
the time they said they would.
1:23
In this scenario,
you've handed control over whether or
1:26
not you will have breakfast to
the person taking your order.
1:29
You expect it to happen, but maybe they
wrote down the wrong phone number, and
1:32
they can't call back or they call,
but your food never makes it to you.
1:35
Perhaps they even deliver the wrong order.
1:39
Now, most of these problems could
be eventually rectified, but
1:41
it's a messy process.
1:44
A process which hands the control
back to you when, and only when,
1:45
breakfast is delivered successfully.
1:50
This is similar to working
with callback functions.
1:52
You're in some ways handing over
control of your function to a callback
1:55
expecting something else to be called and
executed.
1:59
Like callbacks, the process could
be more susceptible to errors and
2:01
harder to debug if something goes wrong.
2:06
In the second scenario, you go to
the breakfast place, put in your order and
2:08
pay for it.
2:12
The cashier then hands you something back
in return, a receipt with an order number
2:13
and a pager that buzzes to let you
know that your order is ready.
2:17
At that point, you may go off and
2:21
do other things while you wait, like grab
a coffee next door or catch up on email.
2:22
You're still waiting for
the pager to buzz.
2:27
But by just having the pager in hand,
you now have more control over whether or
2:29
not you will have breakfast.
2:34
The pager represents a promise that
something is going to happen as a result
2:35
of ordering breakfast.
2:40
With that being said, the promise is
pending because you're waiting, so
2:42
one of two things could happen.
2:45
The pager buzzes,
your order becomes available to you,
2:47
so you pick it up and breakfast is
fulfilled, or the pager buzzes but
2:50
not because your order is ready.
2:55
Instead, it's to inform you that
your food cannot be made and
2:57
the reason why your order
was in some way rejected.
3:00
This second scenario happens to be more
like how promises in JavaScript work.
3:03
A promise, like a pager,
represents a value that can be
3:07
handled at some point later,
like picking up your order.
3:11
While a callback may leave us with
some uncertainties about that value,
3:14
a promise will promise or guarantee
a future value and nothing can change it,
3:18
regardless of whether or
not it comes back fulfilled or rejected.
3:23
Up next, you will learn how to create
a promise, then we'll continue with
3:28
our people in space project by converting
the callback functions to promises.
3:32
You need to sign up for Treehouse in order to download course files.
Sign up