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
If an error occurs, the promise status changes from "pending" to a "rejected" (or failed) state. Promises give you the opportunity to handle the error with a method called catch()
.
Running a JavaScript file with the node
command
If you are not able to run the file promises-breakfast.js
in your Terminal or console using the command node promises-breakfast.js
, you can link the file to index.html
then test the output in your browser's console:
<body>
...
<script src="js/promises-breakfast.js"></script>
<script src="js/callbacks.js"></script>
</body>
Resources
Now I'll teach you how to
handle a rejected Promise.
0:00
If an error occurs, the Promise status
changes from pending to a rejected, or
0:03
failed state.
0:08
The good thing is that promises give you
the opportunity to handle the error with
0:09
a method called catch.
0:13
I'll start by changing resolve to reject.
0:15
And pass it the string, No!
0:20
There was a problem with your order.
0:23
Now I can call the catch
method on breakfastPromise,
0:30
which deals with rejected cases only.
0:33
I can do that by chaining it
to the then method, like so.
0:36
The function passed to catch takes one
argument, which is the rejection reason,
0:43
or the error message to display.
0:48
I'll pass catch an arrow function
that takes the parameter, err, and
0:50
logs it to the console.
0:54
I'll Save my file and
run it in the terminal.
0:59
The reject method changes the status of
the Promise from pending to rejected.
1:03
So this time, instead of then,
catch gets called,
1:08
invoking the function passed to it and
in the Console we see the message, No!
1:11
There was a problem with your order.
1:16
Keep in mind that if you
don't specify reject and
1:18
a catch method to handle a rejected
Promise, the Promise will fail silently.
1:22
Errors will be swallowed up with no
side effects to the rest of your app.
1:27
In other words, the program will continue
to execute as if everything was fine.
1:32
Now that you know how resolve and reject
work, I'm going to write a conditional
1:40
statement inside setTimeout that checks
the true and false value of an order.
1:45
Let's have it say if order is true resolve
1:51
this promise to the value
Your order is ready.
1:56
Come and get it!
2:03
Else, if it's false,
reject the Promise and
2:06
set the rejection reason to
Your order cannot be made.
2:11
This time I'm passing the Error object
to reject versus only a string.
2:20
While it's not required, it does help
with debugging because it will display
2:26
the stack trace alongside
the rejection reason.
2:30
All right, so now to test this,
2:32
I'll initialize a variable
named order to true.
2:36
I'll run the file in the Console,
since order is true,
2:42
the Promise resolves to
Your order is ready.
2:45
Come and get it.
2:48
Now, I'll change order to false and
run the file again.
2:49
This time the Promise is rejected,
so the rejection reason,
2:56
Your order cannot be made, displays in
the Console, followed by the stat trace.
3:00
Good, finally to make your
code more readable and
3:05
the Promise sequence easier to follow,
you can place each then and
3:09
catch call on a separate line like this.
3:14
This approach will come in handy
starting in the next step,
3:19
where you'll chain together
a sequence of Promises.
3:22
You need to sign up for Treehouse in order to download course files.
Sign up