1
00:00:03,980 --> 00:00:08,700
We have just learned about Callback Hell and

2
00:00:08,700 --> 00:00:13,455
how promises can enable us to overcome this issue.

3
00:00:13,455 --> 00:00:19,575
We know that the node MongoDB driver natively supports promises.

4
00:00:19,575 --> 00:00:22,485
So, if you do not specify a callback,

5
00:00:22,485 --> 00:00:26,385
the calls to their functions will return promises.

6
00:00:26,385 --> 00:00:31,445
So, we're going to update our application to make use of promises to

7
00:00:31,445 --> 00:00:38,005
avoid the callback hell issue that we just learned about in the previous lecture.

8
00:00:38,005 --> 00:00:40,644
Getting back to our application,

9
00:00:40,644 --> 00:00:48,035
again taking one more look at the reason the callback hell has developed in our code.

10
00:00:48,035 --> 00:00:52,520
Note that here when we implemented the operations,

11
00:00:52,520 --> 00:00:56,899
we have one operation and then inside the callback of that operation,

12
00:00:56,899 --> 00:01:00,770
then we need to initiate the next operation and inside

13
00:01:00,770 --> 00:01:04,910
the callback of that operation we are initiating the next operation and so on.

14
00:01:04,910 --> 00:01:10,040
So you are ending up with this pyramid like structure here.

15
00:01:10,040 --> 00:01:14,290
One operation inside the callback of another and inside the callback.

16
00:01:14,290 --> 00:01:21,240
Now, this code will pretty soon get unwieldy and complicated to understand.

17
00:01:21,240 --> 00:01:24,760
So that is why we want to transform this code using

18
00:01:24,760 --> 00:01:29,380
the promise support that Mongo DB Driver already provides

19
00:01:29,380 --> 00:01:33,205
for us so that we can instead use promises

20
00:01:33,205 --> 00:01:37,735
rather than using the callback functions as we did in this exercise.

21
00:01:37,735 --> 00:01:42,760
Of course, I did the previous version just to illustrate to you how we can

22
00:01:42,760 --> 00:01:48,595
land up with callback hell and how we can use promises to overcome this issue.

23
00:01:48,595 --> 00:01:53,560
So, before we update the index.js file,

24
00:01:53,560 --> 00:01:59,989
let me go into the operations.jsfile and then we will update this first.

25
00:01:59,989 --> 00:02:06,950
So in here, you'll notice that when we are calling this function here,

26
00:02:06,950 --> 00:02:14,630
we are passing in the second parameter which is a callback function here.

27
00:02:14,630 --> 00:02:18,210
So, because we're going to be using promises here.

28
00:02:18,210 --> 00:02:21,545
So I'm just going to delete that callback function and then

29
00:02:21,545 --> 00:02:25,744
since this call to the insert will anyway return promises,

30
00:02:25,744 --> 00:02:29,710
I'm just going to return the promise from this function.

31
00:02:29,710 --> 00:02:33,605
So note how the code got simplified.

32
00:02:33,605 --> 00:02:36,090
Similarly for the second one,

33
00:02:36,090 --> 00:02:42,740
I will simply remove this callback function and

34
00:02:42,740 --> 00:02:47,900
then return the promise that is

35
00:02:47,900 --> 00:02:55,580
being returned by this and then we'll handle that in the code in index.js.

36
00:02:55,580 --> 00:02:58,495
Similarly for the delete one also,

37
00:02:58,495 --> 00:03:08,280
I am going to remove the callback and then return that crash and also for uptade.

38
00:03:10,600 --> 00:03:16,730
For update, we're going to remove the callback that we have given

39
00:03:16,730 --> 00:03:22,564
here and then return the promise.

40
00:03:22,564 --> 00:03:25,100
So this way all these four functions are going

41
00:03:25,100 --> 00:03:27,575
to be returning the promise that is already returned

42
00:03:27,575 --> 00:03:33,110
by these calls to the MongoDB driver functions.

43
00:03:33,110 --> 00:03:34,760
Now once we have completed that,

44
00:03:34,760 --> 00:03:38,210
let's go back to index.js.

45
00:03:38,210 --> 00:03:44,090
In index.js, I'm going to again update this function here.

46
00:03:44,090 --> 00:03:53,105
So using promises will say MongoClient.connect(url) and then

47
00:03:53,105 --> 00:04:03,470
we will replace this by saying "then" and this receives only the db as their parameter.

48
00:04:03,470 --> 00:04:10,390
And inside here, we'll handle the rest and also we can

49
00:04:10,390 --> 00:04:18,350
do notice that this particular function closes the "Then" here and the promise,

50
00:04:18,350 --> 00:04:20,505
the second part to the promise,

51
00:04:20,505 --> 00:04:22,845
we can handle the error.

52
00:04:22,845 --> 00:04:28,055
We can say consolelog.

53
00:04:28,055 --> 00:04:33,755
So we can use the catch of the promise also to catch the errors.

54
00:04:33,755 --> 00:04:36,275
So that way, we have caught the errors.

55
00:04:36,275 --> 00:04:39,815
Now, let's improve the code inside here.

56
00:04:39,815 --> 00:04:45,530
So inside here we are doing DB operation insert document,

57
00:04:45,530 --> 00:04:49,685
and instead of calling this function here,

58
00:04:49,685 --> 00:04:59,115
what I'm going to do is to turn this into the "then" here.

59
00:04:59,115 --> 00:05:06,870
So I'm going to close off this and say.then result.

60
00:05:06,870 --> 00:05:08,800
So inside those result,

61
00:05:08,800 --> 00:05:11,600
we're going to print out the console log and then.

62
00:05:11,600 --> 00:05:20,630
So I'm going to close this off here and then we'll

63
00:05:20,630 --> 00:05:25,505
close the "then" here and then

64
00:05:25,505 --> 00:05:31,950
this will be attached to again a "then" here.

65
00:05:32,600 --> 00:05:36,640
So you see that I am chaining two "thens"

66
00:05:36,640 --> 00:05:40,070
one into the other here and then inside of us then,

67
00:05:40,070 --> 00:05:47,570
we'll do a console log and then we'll do a return of the next operation.

68
00:05:47,570 --> 00:05:51,390
But then, for this operation,

69
00:05:52,540 --> 00:06:02,070
I am going to close off this and close this

70
00:06:02,070 --> 00:06:06,920
then and then this call back again now will

71
00:06:06,920 --> 00:06:13,320
be handled inside a "then" function here.

72
00:06:13,460 --> 00:06:20,510
So note how you have the first function and in the "then" we are calling

73
00:06:20,510 --> 00:06:27,870
the dboper find documents and that will return and this will return a promise.

74
00:06:27,870 --> 00:06:30,410
That promise will be handled by this then.

75
00:06:30,410 --> 00:06:35,720
So we are chaining two thens together and then again chaining one more "then" here.

76
00:06:35,720 --> 00:06:47,005
Let me reinvent the code here and then we will return this here and

77
00:06:47,005 --> 00:06:57,610
this again will close off and close that "then"

78
00:06:57,610 --> 00:07:07,850
and then this gets enclosed inside the next "then" and we

79
00:07:07,850 --> 00:07:13,280
will call the next function here "that

80
00:07:13,280 --> 00:07:21,130
db.dropCollection('Dishes' and close off "this then" and the next one here.

81
00:07:26,660 --> 00:07:31,390
We'll handle the "then" of the result here.

82
00:07:39,650 --> 00:07:48,690
Return dbclose and all

83
00:07:48,690 --> 00:07:56,785
these here can be now removed because they are no longer needed.

84
00:07:56,785 --> 00:08:04,520
And then finally if there is an error will catch the error this function.

85
00:08:04,520 --> 00:08:12,930
So I'm just going to copy this and then paste it here. That is it.

86
00:08:12,930 --> 00:08:14,755
With this change now,

87
00:08:14,755 --> 00:08:20,220
your code is a lot more easier to handle here.

88
00:08:20,220 --> 00:08:21,510
So as you can see,

89
00:08:21,510 --> 00:08:24,560
you have the MongoClient.connect which

90
00:08:24,560 --> 00:08:28,040
returns a promise and inside the handling of that promise,

91
00:08:28,040 --> 00:08:31,700
they're calling these methods and one after another they are

92
00:08:31,700 --> 00:08:35,490
each returning the promise and then you're chaining them using the the.

93
00:08:35,490 --> 00:08:41,635
This chord structure is lot more easier to follow than what we had implemented earlier.

94
00:08:41,635 --> 00:08:46,280
So using promises we have literally turned it around and then avoided

95
00:08:46,280 --> 00:08:51,250
the callback hell that we saw in the earlier version of this application.

96
00:08:51,250 --> 00:08:53,590
So let's save the changes to both the

97
00:08:53,590 --> 00:08:58,715
index.jsn.operation.js and then take a look at this application.

98
00:08:58,715 --> 00:09:00,840
Again going to the terminal,

99
00:09:00,840 --> 00:09:03,105
at the prompt type "npm

100
00:09:03,105 --> 00:09:09,515
start" and you will see that your application runs exactly as before,

101
00:09:09,515 --> 00:09:12,200
it inserts the document, finds the document,

102
00:09:12,200 --> 00:09:15,080
updates the document and then finds

103
00:09:15,080 --> 00:09:18,530
the updated document and then closes the database of the act.

104
00:09:18,530 --> 00:09:23,630
So using promises we have restructured the code to be lot more easier and

105
00:09:23,630 --> 00:09:29,945
avoided the callback hell that we saw in the previous version of this application.

106
00:09:29,945 --> 00:09:33,705
With this, we complete this exercise.

107
00:09:33,705 --> 00:09:36,470
In this exercise, you have seen how we can make use

108
00:09:36,470 --> 00:09:39,590
of promises to avoid the callback hell.

109
00:09:39,590 --> 00:09:42,920
This is a good time for you to do a git commit with

110
00:09:42,920 --> 00:09:47,950
the message callback hell and promise.