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.

Promises

Promises - Node.js Tutorial

From the course: Learning Node.js

Start my 1-month free trial

Promises

- [Instructor] Promises give us another option of how to work with asynchronous code. Promises return an object which promise to do some work. This object has separate callbacks for success and for failures. This lets us work with asynchronous code in a much more synchronous way. A really nice feature is that Promises can be combined into dependency chains. To demonstrate these features, let's convert censor example to use Promises so that we can see how it cleans up our code a bit. Let's scroll down to our message post function, and on line 28, instead of passing a callback of the first parameter, we'll pass nothing, and then we'll add .then, and this will execute if there are no errors. And then we can place our callback inside the then function. And so now, this function, as a callback, is placed inside then. Since we got rid of the error parameter, we'll need to check for errors using another method. We can chain on a catch function at the end. And then we'll pass on a callback to…

Contents