Digging Into Node.js

Asynchronous readFile

Kyle Simpson

Kyle Simpson

You Don't Know JS
Digging Into Node.js

Check out a free preview of the full Digging Into Node.js course

The "Asynchronous readFile" Lesson is part of the full, Digging Into Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Kyle discusses asynchronicity in Node, then modifies the call to read the file to make the call asynchronously with readFile.

Preview
Close

Transcript from the "Asynchronous readFile" Lesson

[00:00:00]
>> Kyle Simpson: Now we're using a synchronous form of this read file, because it's really easy to use synchronous commands. But you probably know that one of the things that Node was designed to be is asynchronous everywhere at least as much as possible. That's not entirely true because from the very beginning, Node had a synchronous require, so the truth is, actually, that Node said, everything that’s not part of the startup should be asynchronous.

[00:00:32]
Well, in a command line script, everything is part of the startup, basically. So technically it doesn't matter if we stay synchronous, because the chances of you running thousands of instances of your command line script all at the same time, all against the same part of the file system, and blocking each other or whatever, the chances of that are really low.

[00:00:54]
So it would be okay for us if we were just doing some quick and dirty scripting, to use the synchronous forms. But since we're gonna be doing lots of other things throughout the rest of the exercise, we really want to get experience with practicing asynchronous stuff. So if we take off the sync part, we're now gonna be using the asynchronous form.

[00:01:15]
And the asynchronous form of readFile expects a callback. It's not gonna give us our contents like that. It's going to expect a callback. So we can say onContents,
>> Kyle Simpson: And there's one other little nuance, which is that Node for the most part uses a standard for its callback signatures that the first parameter is always the error parameter, typically named err.

[00:01:48]
And we always need to check to make sure that there wasn't an error, so we're gonna say if(err), then we probably just want to pass that error directly along to our print error handler. But error here is gonna be another buffer, so we wanna go ahead, I mean, it's gonna be an object, so we wanna call ahead and call toString on it so that will actually print something useful for the reader.

[00:02:19]
And if it wasn't an error, then we can write out thoser contents.
>> Kyle Simpson: When we run it with an existent file, no change. But now what happens if I give it a file name that doesn't exist? Well, now we get an error message. It's not a great error message, but it does at least tell us no such file or directory, it was trying to access this file and it couldn't find it.

[00:02:48]
So it's slightly more graceful handling.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now