1 00:00:00,424 --> 00:00:03,485 Now I'll teach you how to handle a rejected Promise. 2 00:00:03,485 --> 00:00:08,485 If an error occurs, the Promise status changes from pending to a rejected, or 3 00:00:08,485 --> 00:00:09,486 failed state. 4 00:00:09,486 --> 00:00:13,737 The good thing is that promises give you the opportunity to handle the error with 5 00:00:13,737 --> 00:00:15,013 a method called catch. 6 00:00:15,013 --> 00:00:18,299 I'll start by changing resolve to reject. 7 00:00:20,259 --> 00:00:23,493 And pass it the string, No! 8 00:00:23,493 --> 00:00:24,879 There was a problem with your order. 9 00:00:30,426 --> 00:00:33,936 Now I can call the catch method on breakfastPromise, 10 00:00:33,936 --> 00:00:36,510 which deals with rejected cases only. 11 00:00:36,510 --> 00:00:39,992 I can do that by chaining it to the then method, like so. 12 00:00:43,190 --> 00:00:48,051 The function passed to catch takes one argument, which is the rejection reason, 13 00:00:48,051 --> 00:00:50,033 or the error message to display. 14 00:00:50,033 --> 00:00:54,139 I'll pass catch an arrow function that takes the parameter, err, and 15 00:00:54,139 --> 00:00:55,514 logs it to the console. 16 00:00:59,325 --> 00:01:03,590 I'll Save my file and run it in the terminal. 17 00:01:03,590 --> 00:01:08,156 The reject method changes the status of the Promise from pending to rejected. 18 00:01:08,156 --> 00:01:11,547 So this time, instead of then, catch gets called, 19 00:01:11,547 --> 00:01:16,730 invoking the function passed to it and in the Console we see the message, No! 20 00:01:16,730 --> 00:01:18,691 There was a problem with your order. 21 00:01:18,691 --> 00:01:22,002 Keep in mind that if you don't specify reject and 22 00:01:22,002 --> 00:01:27,349 a catch method to handle a rejected Promise, the Promise will fail silently. 23 00:01:27,349 --> 00:01:32,300 Errors will be swallowed up with no side effects to the rest of your app. 24 00:01:32,300 --> 00:01:37,219 In other words, the program will continue to execute as if everything was fine. 25 00:01:40,356 --> 00:01:45,766 Now that you know how resolve and reject work, I'm going to write a conditional 26 00:01:45,766 --> 00:01:51,113 statement inside setTimeout that checks the true and false value of an order. 27 00:01:51,113 --> 00:01:56,731 Let's have it say if order is true resolve 28 00:01:56,731 --> 00:02:03,002 this promise to the value Your order is ready. 29 00:02:03,002 --> 00:02:04,328 Come and get it! 30 00:02:06,492 --> 00:02:11,535 Else, if it's false, reject the Promise and 31 00:02:11,535 --> 00:02:17,499 set the rejection reason to Your order cannot be made. 32 00:02:20,772 --> 00:02:26,184 This time I'm passing the Error object to reject versus only a string. 33 00:02:26,184 --> 00:02:30,306 While it's not required, it does help with debugging because it will display 34 00:02:30,306 --> 00:02:32,936 the stack trace alongside the rejection reason. 35 00:02:32,936 --> 00:02:36,533 All right, so now to test this, 36 00:02:36,533 --> 00:02:42,144 I'll initialize a variable named order to true. 37 00:02:42,144 --> 00:02:45,535 I'll run the file in the Console, since order is true, 38 00:02:45,535 --> 00:02:48,290 the Promise resolves to Your order is ready. 39 00:02:48,290 --> 00:02:49,527 Come and get it. 40 00:02:49,527 --> 00:02:54,093 Now, I'll change order to false and run the file again. 41 00:02:56,147 --> 00:03:00,084 This time the Promise is rejected, so the rejection reason, 42 00:03:00,084 --> 00:03:05,324 Your order cannot be made, displays in the Console, followed by the stack trace. 43 00:03:05,324 --> 00:03:09,050 Good, finally to make your code more readable and 44 00:03:09,050 --> 00:03:14,019 the Promise sequence easier to follow, you can place each then and 45 00:03:14,019 --> 00:03:17,051 catch call on a separate line like this. 46 00:03:19,246 --> 00:03:22,382 This approach will come in handy starting in the next step, 47 00:03:22,382 --> 00:03:25,276 where you'll chain together a sequence of Promises.