We have just learned about Callback Hell and
how promises can enable us to overcome this issue.
We know that the node MongoDB driver natively supports promises.
So, if you do not specify a callback,
the calls to their functions will return promises.
So, we're going to update our application to make use of promises to
avoid the callback hell issue that we just learned about in the previous lecture.
Getting back to our application,
again taking one more look at the reason the callback hell has developed in our code.
Note that here when we implemented the operations,
we have one operation and then inside the callback of that operation,
then we need to initiate the next operation and inside
the callback of that operation we are initiating the next operation and so on.
So you are ending up with this pyramid like structure here.
One operation inside the callback of another and inside the callback.
Now, this code will pretty soon get unwieldy and complicated to understand.
So that is why we want to transform this code using
the promise support that Mongo DB Driver already provides
for us so that we can instead use promises
rather than using the callback functions as we did in this exercise.
Of course, I did the previous version just to illustrate to you how we can
land up with callback hell and how we can use promises to overcome this issue.
So, before we update the index.js file,
let me go into the operations.jsfile and then we will update this first.
So in here, you'll notice that when we are calling this function here,
we are passing in the second parameter which is a callback function here.
So, because we're going to be using promises here.
So I'm just going to delete that callback function and then
since this call to the insert will anyway return promises,
I'm just going to return the promise from this function.
So note how the code got simplified.
Similarly for the second one,
I will simply remove this callback function and
then return the promise that is
being returned by this and then we'll handle that in the code in index.js.
Similarly for the delete one also,
I am going to remove the callback and then return that crash and also for uptade.
For update, we're going to remove the callback that we have given
here and then return the promise.
So this way all these four functions are going
to be returning the promise that is already returned
by these calls to the MongoDB driver functions.
Now once we have completed that,
let's go back to index.js.
In index.js, I'm going to again update this function here.
So using promises will say MongoClient.connect(url) and then
we will replace this by saying "then" and this receives only the db as their parameter.
And inside here, we'll handle the rest and also we can
do notice that this particular function closes the "Then" here and the promise,
the second part to the promise,
we can handle the error.
We can say consolelog.
So we can use the catch of the promise also to catch the errors.
So that way, we have caught the errors.
Now, let's improve the code inside here.
So inside here we are doing DB operation insert document,
and instead of calling this function here,
what I'm going to do is to turn this into the "then" here.
So I'm going to close off this and say.then result.
So inside those result,
we're going to print out the console log and then.
So I'm going to close this off here and then we'll
close the "then" here and then
this will be attached to again a "then" here.
So you see that I am chaining two "thens"
one into the other here and then inside of us then,
we'll do a console log and then we'll do a return of the next operation.
But then, for this operation,
I am going to close off this and close this
then and then this call back again now will
be handled inside a "then" function here.
So note how you have the first function and in the "then" we are calling
the dboper find documents and that will return and this will return a promise.
That promise will be handled by this then.
So we are chaining two thens together and then again chaining one more "then" here.
Let me reinvent the code here and then we will return this here and
this again will close off and close that "then"
and then this gets enclosed inside the next "then" and we
will call the next function here "that
db.dropCollection('Dishes' and close off "this then" and the next one here.
We'll handle the "then" of the result here.
Return dbclose and all
these here can be now removed because they are no longer needed.
And then finally if there is an error will catch the error this function.
So I'm just going to copy this and then paste it here. That is it.
With this change now,
your code is a lot more easier to handle here.
So as you can see,
you have the MongoClient.connect which
returns a promise and inside the handling of that promise,
they're calling these methods and one after another they are
each returning the promise and then you're chaining them using the the.
This chord structure is lot more easier to follow than what we had implemented earlier.
So using promises we have literally turned it around and then avoided
the callback hell that we saw in the earlier version of this application.
So let's save the changes to both the
index.jsn.operation.js and then take a look at this application.
Again going to the terminal,
at the prompt type "npm
start" and you will see that your application runs exactly as before,
it inserts the document, finds the document,
updates the document and then finds
the updated document and then closes the database of the act.
So using promises we have restructured the code to be lot more easier and
avoided the callback hell that we saw in the previous version of this application.
With this, we complete this exercise.
In this exercise, you have seen how we can make use
of promises to avoid the callback hell.
This is a good time for you to do a git commit with
the message callback hell and promise.