Sei sulla pagina 1di 2

We have just learned about Callback Hell and how Promises can enable us to overcome

this issue. We know that the node Mongo DB driver natively supports Promises. And
so if you do not specify a callback, the calls to their functions will return from
lists. 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. Inside the callback of that operation,
we are initiating the next operation, and so on. 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. 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 line up with Callback
Hell, and how we can use Promises to overcome this issue. Now, before we update the
index.js file, let me go into the operations.js file and then we will update this
first. In here, you'll notice that when we are calling this functions here, we are
passing in the second parameter, which is a callback function here. Because we are
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. Note how that code got
simplified. Similarly, for that 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'm
going to remove the callback, and then return that one and also for update. For
update, we're going to remove the callback that we have given here, and then return
the Promise. This way, all these four functions are going to be returning the
Promise that is already returned by these calls to the Mongo DB driver functions.
Now once we have completed that, let's go back to index.js and index.js, I'm going
to again update this function here, so we'll see using Promises, we'll say
Mongo.Client.connect(url), and then we will replace this by saying "then" and this
receives only the DB as the parameter. And inside here, we'll handle the rest. And
also, we can begin notice that this particular function closes that "then" here.
And the Promise, the second part to the Promise, we can handle the error. We can
see console log(err) here. And also, we can add in a catch((err), We can use the
"then" catch of that Promise also to catch the errors. That way, you have caught
the errors. Now, let's improve the code inside here. Inside here, we are doing DB
operation insertDocument, and instead of calling this function here, what I'm going
to do is to turn this into "then" here. I'm going to close off this and then
say.then((result). Inside this 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. You see that I am changing two
"then"s, one into the other here. And then inside this "then", we'll do a console
log and then we'll do a return of that next operation. But then, for this
operation, I'm going to close off this, and close this "then", and then this
callback again, now will be handled inside a "then" function here. Note how you
have that first function and then into "then", we are calling the
dboper.findDocuments, and that will return, and doesn't return a Promise. That
Promise will be handled by this "then". We are chaining two "then's" together and
that again, chaining one more "then" in here, let me re-indent 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 the db.dropCollection("dishes"), and close off this "then", and
the next one here. We'll handle the "then" of the result here, return db.close, and
all of this here can be now removed because they are no longer needed. And then
finally, if there is an error, we'll catch the error with this function. 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. As you can see, you have the
MongoClient.connect, which returns a Promise, and inside the handling of that
promise, you're calling these metrics. And one after another, they are each
returning the Promise and then you are changing them using the "then". This code
structure is a lot more easier to follow than what we had implemented earlier.
Using Promises, we have literally turned it around and avoided the Callback Hell
that we saw in the earlier version of this application. Let's save the changes to
both the index.js and operations.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 updated document and then closes the
database, the int. Using Promises, we have restructured the code to be a 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.

Potrebbero piacerti anche