From the course: Learning Node.js

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Finally

Finally

- [Instructor] We have our try and catch blocks, and the third optional part is the finally block. This gets executed regardless if the try catch block fails or passes. For now let's add the finally block to our app.post. There are some cases in which you would want to use a finally block, but in most cases you're probably just going to end up using try and catch. An example could be if you're submitting something to a log. So, in our case, if we had a logger we could add an entry that the message post end point was called, or if you're using a resource that you need to shut down, for example, in the case of a database you might open a connection, post something, and then you might have to close it in the finally. For now, just so we can explore the concept of the finally block, I'll change this into a console.log. Then let's open up our Terminal. And now let's try posting a new message. Let's go look at our Terminal, and we can see that we're getting our message post called…

Contents