From the course: Learning Node.js

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Nested callbacks

Nested callbacks - Node.js Tutorial

From the course: Learning Node.js

Start my 1-month free trial

Nested callbacks

- [Instructor] In asynchronous programming, there is something many programmers refer to as callback hell. It can happen to nearly everyone and it creates very difficult code. Let's take a look at what it is exactly and how we can avoid it. Let's create a very basic bad word filter for our post message function. There are better ways to make this, but we will purposefully make the callbacks nested so that you can understand the teaching point. So let's go down to our message post function on line 25. If a message was successfully saved and there was no error, let's check for bad words inside the database. So we'll use message.findOne and we'll check for a message that has the word badword in it, and then let's pass a callback with an error and the censored word that it finds. If there is a censored word, let's console.log censored words found, and we'll display censored. And if we did find a censored word, let's remove it. We'll use message.remove and then pass in the ID. So we'll…

Contents