1
00:00:02,130 --> 00:00:04,400
Now we also learn about promises,

2
00:00:04,400 --> 00:00:08,010
often an alternative to working with callback functions,

3
00:00:08,010 --> 00:00:10,280
which you pass directly to the method

4
00:00:10,280 --> 00:00:12,560
that starts the async operation.

5
00:00:12,560 --> 00:00:15,790
Now in both scenarios, things can go wrong.

6
00:00:15,790 --> 00:00:17,880
Errors could occur.

7
00:00:17,880 --> 00:00:20,200
And since async operations

8
00:00:20,200 --> 00:00:23,160
work such that an operation is started

9
00:00:23,160 --> 00:00:25,290
and the code thereafter executes

10
00:00:25,290 --> 00:00:27,410
without waiting for that result,

11
00:00:27,410 --> 00:00:32,409
you can't use try catch here around your promise.

12
00:00:33,570 --> 00:00:35,720
So we can't do that here.

13
00:00:35,720 --> 00:00:37,170
I mean, we can write this code,

14
00:00:37,170 --> 00:00:39,043
but it won't work as intended.

15
00:00:41,270 --> 00:00:45,030
And we also can't do that if we use a callback function.

16
00:00:45,030 --> 00:00:49,810
Wrapping this with try catch also would not work as intended

17
00:00:49,810 --> 00:00:54,050
because try would only check if calling read file succeeds

18
00:00:54,050 --> 00:00:58,163
and not whether the result of read file is a success.

19
00:00:59,500 --> 00:01:02,250
That's why in case of the callback approach,

20
00:01:02,250 --> 00:01:04,540
we get this error parameter,

21
00:01:04,540 --> 00:01:08,280
which tells us whether an error occurred or not.

22
00:01:08,280 --> 00:01:10,580
And then we can always use a if check

23
00:01:10,580 --> 00:01:15,580
to see if error is set, if it's truthy as we learned,

24
00:01:16,490 --> 00:01:19,390
and then do something with it.

25
00:01:19,390 --> 00:01:21,790
That's something we can do to handle errors

26
00:01:21,790 --> 00:01:24,140
when working with callbacks.

27
00:01:24,140 --> 00:01:27,320
And I'll leave it at that dummy example here

28
00:01:27,320 --> 00:01:30,360
because we'll see better, more concrete examples

29
00:01:30,360 --> 00:01:32,233
in the later course sections.

30
00:01:33,500 --> 00:01:36,590
In case of promises, it's a bit different.

31
00:01:36,590 --> 00:01:38,810
Here in this anonymous function,

32
00:01:38,810 --> 00:01:42,190
we only get the data if the operation here

33
00:01:42,190 --> 00:01:44,223
reading the file succeeded.

34
00:01:45,360 --> 00:01:47,260
If that operation failed,

35
00:01:47,260 --> 00:01:50,880
we instead can add another method here

36
00:01:50,880 --> 00:01:54,723
to this promise chain and that's the catch method.

37
00:01:55,840 --> 00:01:59,920
Promises have a then method to handle the success case

38
00:01:59,920 --> 00:02:02,710
and catch to handle any errors

39
00:02:02,710 --> 00:02:05,403
that might've occurred in a previous promise.

40
00:02:06,610 --> 00:02:08,759
Catch also takes a function,

41
00:02:08,759 --> 00:02:11,510
but a function which gets that error object

42
00:02:11,510 --> 00:02:13,963
that describes the error that occurred.

43
00:02:15,190 --> 00:02:17,880
And then here we can console.log that error

44
00:02:17,880 --> 00:02:20,543
or handle it in any way we wanna handle it.

45
00:02:21,700 --> 00:02:23,640
And that's how you do handle errors

46
00:02:23,640 --> 00:02:26,190
with promises or callbacks.

47
00:02:26,190 --> 00:02:29,890
Again, we'll see better a little bit more concrete examples

48
00:02:29,890 --> 00:02:31,730
a little bit later in the course,

49
00:02:31,730 --> 00:02:33,670
but that's already the knowledge you need

50
00:02:33,670 --> 00:02:37,713
to understand how error handling will work later on.

