1
00:00:03,920 --> 00:00:08,675
In this lecture and the exercise that follows,

2
00:00:08,675 --> 00:00:12,000
we'll talk specifically about Callback Hell.

3
00:00:12,000 --> 00:00:15,330
The problem that arises when you write

4
00:00:15,330 --> 00:00:19,980
callbacks in node and especially nested callbacks and node.

5
00:00:19,980 --> 00:00:26,795
We'll also look at promises as one way of addressing the Callback Hell problem.

6
00:00:26,795 --> 00:00:34,340
To give you an idea of what the typical Callback Hell problem will result in,

7
00:00:34,340 --> 00:00:40,115
let's revisit the index.js file from the previous exercise.

8
00:00:40,115 --> 00:00:44,020
As I was talking about it in the previous exercise

9
00:00:44,020 --> 00:00:47,959
you notice that for each of the operations that we perform,

10
00:00:47,959 --> 00:00:53,990
the subsequent operation is included inside the callback function here

11
00:00:53,990 --> 00:01:00,320
and so on until you form a pyramid like structure go again.

12
00:01:00,320 --> 00:01:06,160
So you see this pyramid like structure arising within your code.

13
00:01:06,160 --> 00:01:11,540
Now imagine that you need to perform multiple nested operations like this,

14
00:01:11,540 --> 00:01:13,850
your code will become pretty soon,

15
00:01:13,850 --> 00:01:18,085
very complex, and not so easy to decipher.

16
00:01:18,085 --> 00:01:22,240
Now this is what in the node.js world,

17
00:01:22,240 --> 00:01:25,830
they refer to as Callback Hell.

18
00:01:25,960 --> 00:01:30,845
There have been several ways of mitigating the problem that arises.

19
00:01:30,845 --> 00:01:35,690
Now you can completely avoid this because of the fact

20
00:01:35,690 --> 00:01:38,300
that you have certain operations that need to

21
00:01:38,300 --> 00:01:41,480
complete before the next operation can be initiated,

22
00:01:41,480 --> 00:01:47,765
but we can rearrange the code in a way to mitigate this problem.

23
00:01:47,765 --> 00:01:52,080
Promises being one such way of addressing this.

24
00:01:52,080 --> 00:01:56,015
So to summarize what we have just discussed,

25
00:01:56,015 --> 00:01:59,435
heavily nested callback code,

26
00:01:59,435 --> 00:02:02,990
causes the Callback Hell problem and it

27
00:02:02,990 --> 00:02:07,010
results from our tendency to write programs top-down.

28
00:02:07,010 --> 00:02:11,990
We are still hung up with our sequential way of writing code and

29
00:02:11,990 --> 00:02:17,420
so we see it more convenient to write code top to bottom,

30
00:02:17,420 --> 00:02:21,145
and look at it as if it is executing in that order.

31
00:02:21,145 --> 00:02:24,140
Now we can work around the Callback Hell problem by

32
00:02:24,140 --> 00:02:27,460
not using anonymous functions for the callbacks but instead,

33
00:02:27,460 --> 00:02:30,870
declaring those functions with specific names,

34
00:02:30,870 --> 00:02:35,630
and then avoid the way we write the code as you saw here.

35
00:02:35,630 --> 00:02:41,190
That is one of the approaches that people take to deal with the Callback Hell problem.

36
00:02:41,190 --> 00:02:44,990
There are several other approaches that have been suggested,

37
00:02:44,990 --> 00:02:51,085
links to a couple of articles in this regard are provided in the additional resources.

38
00:02:51,085 --> 00:02:53,720
But in this particular lecture,

39
00:02:53,720 --> 00:02:56,810
I will concentrate on one particular approach that

40
00:02:56,810 --> 00:03:00,275
is used to deal with for the Callback Hell problem,

41
00:03:00,275 --> 00:03:02,370
that is the use of promises.

42
00:03:02,370 --> 00:03:07,345
So we can use promises to tame the Callback Hell problem to quite an extent.

43
00:03:07,345 --> 00:03:12,200
We will look at how promises help us in this regard and in

44
00:03:12,200 --> 00:03:17,180
the exercise we will see how because the NODE-MONGO DB driver already

45
00:03:17,180 --> 00:03:21,665
supports a proxy interface we can leverage that to rewrite our code

46
00:03:21,665 --> 00:03:27,495
to take advantage of promise support in the MONGO DB driver.

47
00:03:27,495 --> 00:03:31,455
Briefly summarizing what a promise is.

48
00:03:31,455 --> 00:03:35,625
A promise is a mechanism that supports asynchronous computation.

49
00:03:35,625 --> 00:03:39,250
So if you have amount of work that needs to be done,

50
00:03:39,250 --> 00:03:43,550
the promise acts as a proxy for a value which is

51
00:03:43,550 --> 00:03:48,100
not known at the moment but the promise is given to you.

52
00:03:48,100 --> 00:03:50,270
But when the value becomes available,

53
00:03:50,270 --> 00:03:53,225
it will be available in the future.

54
00:03:53,225 --> 00:03:56,690
So the promise represents a placeholder for that value.

55
00:03:56,690 --> 00:03:58,795
If the value results correctly,

56
00:03:58,795 --> 00:04:02,240
then your promise results correctly and you can have

57
00:04:02,240 --> 00:04:08,735
a piece of code execute in order to handle the fact that the promise resolved correctly,

58
00:04:08,735 --> 00:04:12,950
if not then you handle the error in that situation.

59
00:04:12,950 --> 00:04:20,060
So, a promise will resolve either into resolve or the rejection of the promise.

60
00:04:20,060 --> 00:04:23,515
A pending promise might either resolve

61
00:04:23,515 --> 00:04:27,105
when the value is correctly obtained so in that case,

62
00:04:27,105 --> 00:04:31,755
it will resolve or what we call as the fulfilling of the promise.

63
00:04:31,755 --> 00:04:33,540
So when the promise is resolved,

64
00:04:33,540 --> 00:04:35,960
then you will have a piece of code that

65
00:04:35,960 --> 00:04:38,760
handles the fact that the promise has been resolved.

66
00:04:38,760 --> 00:04:40,455
If the promise is rejected,

67
00:04:40,455 --> 00:04:43,985
you should also handle that situation

68
00:04:43,985 --> 00:04:48,470
the rejection of the promise correspondingly within your code.

69
00:04:48,470 --> 00:04:52,400
So that is the reason whenever you create a promise,

70
00:04:52,400 --> 00:04:56,680
you only supply the resolve and the reject options from it.

71
00:04:56,680 --> 00:05:02,905
The resolve option is typically handled by the doctor then option for your promise.

72
00:05:02,905 --> 00:05:05,960
So why do we use promises?

73
00:05:05,960 --> 00:05:08,060
Promises are used because it addresses

74
00:05:08,060 --> 00:05:13,130
the callback hell problem to a large extent and promises can be changed.

75
00:05:13,130 --> 00:05:15,860
For example, if you have one promise which in

76
00:05:15,860 --> 00:05:20,690
turn triggers a call to another one which will return a promise.

77
00:05:20,690 --> 00:05:26,525
The handling of the promise can be changed to the handle of the previous promise.

78
00:05:26,525 --> 00:05:28,315
So you can have a bunch of,

79
00:05:28,315 --> 00:05:33,635
then calls that will handle the return of the value.

80
00:05:33,635 --> 00:05:39,930
Now we will see the use of this in the exercise that follows this lecture.

81
00:05:39,930 --> 00:05:42,275
To consume a promise,

82
00:05:42,275 --> 00:05:47,675
you will register an appropriate callback function for

83
00:05:47,675 --> 00:05:50,810
when the consumer of a promise is notified either

84
00:05:50,810 --> 00:05:53,750
of the fulfillment or the rejection of the promise.

85
00:05:53,750 --> 00:05:59,860
So the callbacks are registered through the.then() to the promise.

86
00:05:59,860 --> 00:06:07,040
You will use the.catch() to catch the errors within the return promise.

87
00:06:07,040 --> 00:06:09,845
Now the.then() methods can be chained

88
00:06:09,845 --> 00:06:15,205
together as you will see in the exercise that follows.

89
00:06:15,205 --> 00:06:19,730
As an example, you would normally handle a promise by

90
00:06:19,730 --> 00:06:26,250
chaining the.then() and the.catch() to the promise value.