1
00:00:02,070 --> 00:00:08,730
So now that we saw how we resolve a promise and handle this value in the then callback, let's reject it.

2
00:00:08,970 --> 00:00:16,380
So for this, I'll comment out resolve here and instead I'll call reject and reject here, I will also pass

3
00:00:16,470 --> 00:00:22,140
a value, this time maybe a Javascript object just to mix it up, where I have an error code maybe, let's

4
00:00:22,140 --> 00:00:27,350
say 500 and a message, an error occurred

5
00:00:27,420 --> 00:00:29,700
and you're totally free to pass back what you want,

6
00:00:29,700 --> 00:00:32,770
you could still just pass a string if you want to do that.

7
00:00:33,090 --> 00:00:39,450
So now we call reject, if we don't change anything else let's save this and let's reload this in the browser

8
00:00:39,690 --> 00:00:41,710
and let's see what happens.

9
00:00:41,970 --> 00:00:50,570
We still execute this right after set timeout but then we get an error that we didn't catch this rejected

10
00:00:50,570 --> 00:00:54,880
value basically. We don't get the console log from before,

11
00:00:55,020 --> 00:01:02,640
so we don't execute the then blocks here or actually we do but we only handle success cases here in the

12
00:01:02,640 --> 00:01:10,440
first argument you passed to then. We can also pass a second argument though, you could pass another function

13
00:01:10,530 --> 00:01:15,580
here which will then handle any rejected promise results, so any errors,

14
00:01:15,690 --> 00:01:18,280
of course this argument name is also up to you.

15
00:01:18,300 --> 00:01:23,900
There you could console log err.code and err.messages

16
00:01:24,060 --> 00:01:28,790
and I know that I can access both properties because I pass them here in this object.

17
00:01:28,890 --> 00:01:35,550
If we do this and save and we reload our application again, we see this is executed right after set timeout

18
00:01:35,550 --> 00:01:41,700
again and then after three seconds, we get this log with the error code and our error messages. We

19
00:01:41,700 --> 00:01:45,470
no longer get this error result from before.

20
00:01:45,660 --> 00:01:47,150
We also get undefined,

21
00:01:47,270 --> 00:01:53,160
that's stemming from the second then call where we just don't get the argument because in the error

22
00:01:53,160 --> 00:01:57,230
case, we don't return a value we could process here.

23
00:01:57,330 --> 00:02:01,000
Now this is one way of handling promise rejections,

24
00:02:01,080 --> 00:02:09,510
the more common way is to not use this second argument though. I will quickly comment this out to keep

25
00:02:09,510 --> 00:02:16,970
it for reference but what I want to show here is that you typically wouldn't use that second argument

26
00:02:17,370 --> 00:02:19,370
but to make this super easy to read,

27
00:02:19,620 --> 00:02:22,110
you can simply add catch here,

28
00:02:22,110 --> 00:02:29,340
so I'll add another method to this chain here, catch, I could add it in a new line but let me stick it right

29
00:02:29,340 --> 00:02:30,940
to the end of that line

30
00:02:31,080 --> 00:02:37,160
and here I can now also pass a function which will only be executed in rejected cases,

31
00:02:37,200 --> 00:02:44,050
so never on resolve and there I can also console log err.code and err.messages.

32
00:02:44,070 --> 00:02:50,640
The cool thing about catch is it will catch any errors occurring at any step prior to catch,

33
00:02:50,660 --> 00:02:53,980
so either on the first then block or the second one

34
00:02:54,300 --> 00:03:02,040
and I could also add catch here right after the first then block and then it would only react to errors

35
00:03:02,100 --> 00:03:06,000
which were thrown at this step and would ignore errors at the second step,

36
00:03:06,000 --> 00:03:12,810
so that's possible too. Here I'm now catching errors occurring at any point of time in this

37
00:03:12,810 --> 00:03:13,920
promise chain

38
00:03:14,090 --> 00:03:21,850
and if we now save this and reload this application again, we again see executed right after set timeout

39
00:03:22,140 --> 00:03:25,200
and then we get 500 and an error occured,

40
00:03:25,350 --> 00:03:31,830
you see that the undefined log is missing because catch caught the error immediately and prevented

41
00:03:31,830 --> 00:03:36,660
the promise from executing any other lines because we added it at the end here.

42
00:03:37,080 --> 00:03:39,030
And this is how we can work with promises

43
00:03:39,040 --> 00:03:45,840
in its very basic feature set, then and catch are the two important methods you have to be aware. Use

44
00:03:45,870 --> 00:03:53,850
then to use the normal return values in cases where no error occurred and use catch to handle any

45
00:03:53,850 --> 00:03:55,510
errors which might occur.

46
00:03:55,860 --> 00:04:01,740
Now with that knowledge, let's go back to the promises we already used in this application and try to

47
00:04:01,770 --> 00:04:03,360
understand those a bit better.
