This course has been updated! We now recommend you take the API Design in Node.js, v3 course. Plus, you can join us live online for the recording of the new, API Design in Node.js, v3 workshop.

Check out a free preview of the full API Design in Node.js (using Express & Mongo) course:
The "Consuming Promises" Lesson is part of the full, API Design in Node.js (using Express & Mongo) course featured in this preview video. Here's what you'd learn in this lesson:

Using Node’s file system API, Scott demonstrates how to consume and Node-style callback as a Promise. He wraps the API call in a native Promise and returns a call to the resolve or reject methods.

Get Unlimited Access Now

Transcript from the "Consuming Promises" Lesson

>> [MUSIC]

>> Scott Moss: So that's like the basics of a promise. So this how you would make your promise. Now how you would consume a promise is that, again, because Node has this convention of the node style callbacks where everything is error response. So what we'll do is let's refactor a common function that you will see in node, something like reading the files system.

[00:00:26] So, what we'll do is we'll save our file system equals require file system and what we'll do is we'll start off reading the package json. All right, you got a question? You're stretching, all right. So, we'll say fs.readfile. All right, here we'll say package.json.
>> Scott Moss: So, read this file it takes a call back, back, which means it's error, and then the data that we want, I'm gonna call it file.

[00:00:59] So this is asynchronous. This doesn't happen synchronously, right? So this is gonna happen in the future. Does everybody know what I mean when I say that? Like if I put a console.log underneath this, just to show you. After read file. And I'm just gonna get rid of all this stuff down here so we don't pollute the space.

[00:01:19] And if I put a console.log in here. Which one's gonna log first?
>> Speaker 2: [INAUDIBLE]
>> Scott Moss: Right, after read file's gonna log first.
>> Scott Moss: All right?
>> Scott Moss: So what I can do to refactor this is I can also just return a new promise. But I have to wrap this fs.readfile in that new promise.

[00:01:46] So instead of doing something like console.log.
>> Scott Moss: File.toString, which is the actual file contents Oops.
>> Scott Moss: What I would do is if I wanted to use this in a promise, I'd just make a new promise so I'll say fs or I'll say read file equals a function and it returns a new promise.

[00:02:21] Oops. It takes a call back resolve and a reject. Gotta be in that order. And then what I can do is I can move this stuff up, just go ahead and grab it, bump it up right into here. Then all I have to do is, now, I can just do like a if statement or itinerary.

[00:02:46] So I can just do like, yeah, it's gonna return. If there's an error, then let's just reject the error. There's not an error, let's just resolve the file. And I'll just do two string. Right? So it's gonna either or. If there's an error, reject the error. If there's not an error then we have to, and if there's not an error then it's resolve whatever the file is.

[00:03:15] So now when I do it I can just come down here and say readFile .then and I'll have the file here or I might have an error here, depending on what readFile returns.
>> Scott Moss: Oops. Right, so now if I run this, it's the same thing. Same output.
>> Scott Moss: Everybody see that, everybody get that?

[00:03:57] If you don't get it that's fine, this stuff is not easy. Promise took me awhile to understand. And this is just the basics, most promise libraries have like 100 methods that do a hundred different things. Which is ridiculous but these are the basics. And then usually what you would do is you have another third one that's rarely used but I think is very important and is usually called finally or maybe sometimes is called done.

[00:04:22] But what happens on this one is this method will always get called no matter if there was an error or a resolve. So this is like a great place to update your UI if you were doing front end stuff, so all right make a call to the server.

[00:04:33] So what you'll do is let's say this is a call to the server on the client. You start a loading animation here. Right and then you're like .then get the data .catch log to err. But .finally, I don't care if there's an err or not. Stop the loading animation right?

[00:04:52] So this is where these situations like that were as always great. Where it's like guaranteed this is gonna call no matter if resolve or not. Yes?
>> Speaker 2: You might get to this but they're gonna ask me, how would handle nested promises and avoid the call back pyramid
>> Scott Moss: Yep, that's exactly what I was about to get into.

[00:05:08] You just read my mind.
>> Scott Moss: Yes.
>> Speaker 3: So do you normally use the built-in library, or do you normally use something else like queue?
>> Scott Moss: Well the built-in library's brand new.
>> Speaker 3: It's brand new.
>> Scott Moss: Yeah, it's in this project 2015. I used to love Q. I still like Q, but now I use Bloomberg.

[00:05:33] It's probably the most popular, fastest one. But then is pretty easy. It has all the methods you need. Bloomberg has just a grab bag of more than what you ever need.
>> Speaker 3: Why is Bloomberg, why do you like it better than Q?
>> Scott Moss: It was trending.
>> Speaker 3: [LAUGH]

>> Scott Moss: I wanted to do something new, it's faster. It's performance tested to be faster, and it had some methods on it that I liked. I just like one method in particular where it can permissify, any Node style callback, which I thought was pretty awesome.
>> Speaker 3: Okay.
>> Scott Moss: Well, so can Q but the syntax is weird.

[00:06:07] Cool, so any questions on this so far, not really on just nested call backs and different tricks and stuff?