learnyounode Lesson 4 – My First Async I/O!

(Last Updated On: 2017-11-05)

My First Async I/O! introduces us to asynchronous input/output.  The node.js framework is built on the idea of using asynchronous operations rather than synchronous operations like we saw in lesson 3.  In fact, node is described as an asynchronous event driven JavaScript runtime on nodejs.org, the official site for node.js.

The previous lessons were not particularly difficult.  However, asynchronous programming is challenging.  This is probably the most important lesson in learnyou node.  Everything moving forward builds on this lesson.  Understanding asynchronous processing is critical to programming with node.

The lesson hint suggest reading up on callbacks in Max Ogden’s Art of Node article on Git.  However, I recommend taking the time to read the who thing.  In fact, the solution that I provide for this lesson is built upon Ogden’s getNumber example.

Callbacks are part of the JavaScript language.  A callback is a type of function that executes at a later time.  Imagine you are a contractor building a house.  You need to have the cabinets installed, the walls painted, and molding installed along the walls.  The walls will be white, and the cabinets and trim will be oak.  You find a painter, a cabinet installer, and a finisher to do the work, but you obviously can’t have all three of them do their jobs at the same time.  On top of that, the order that the tasks are completed matter.  If the cabinets and molding are installed first, the painter might accidentally get white paint on the oak woodwork.  On the other hand, if the cabinet installer and finisher try to do their jobs right after the painter leaves, they might ruin the paint.  You, as the contractor tell the painter to call the cabinet installer and finisher back after he is done has done and the paint has dried.  That is a callback.  One function tells another function it will call the other function back once it does its job.

An alternative to callbacks are promises.  Promises are outside of the scope of this example.  However, if you would like to learn more about them, check out What Pinky and the Brain can teach us about Promises and Currys.

In order to print the number of lines in our file, we need to use the console.log() method inside of a callback function.  If we place it outside of a callback function, the console.log() and fs.readFile() methods will run at the same time.  However, console.log() will complete significantly faster because it does not need to talk to the hard drive.  As a result, console.log() will print ‘undefined’ to the console instead of the number of lines in the file.

Official Solution

Click to show/hide solution


As you can see in the official solution above, console.log is placed within a callback function.  Let’s break down the structure of the official solution.  You can see that ‘fs’ is required just like the previous exercise.  The third index of the process object’s argv property is passed to a variable called file.  The fs.readFile() method receives two arguments, file, and a callback function() {}.  The callback function also has two arguments, err and contents.  Inside of the callback function, contents are passed to the variable lines, and converted to a string with the toString() method.  Then the string is split at each new line with the split() method.  Next we get the length of the split method minus one.  Finally, console.log() prints the contents of lines to the console.  Note how the callback is encapsulated in the fs.readFile() method.

The official solution shows one of the most common methods of employing callbacks that I have observed in the nodesphere.  If you wanted to do more than one thing with the results of your function, you could keep adding functions inside of your function.  However, you could easily find yourself in a place known as callback hell.  A deeply nested set of functions that are difficult to read and understand.  Imagine callbacks nested with callbacks, nested within callbacks to the point where the code starts to look like an ascii image of the rocky mountains.  To avoid this, you can break your functions into multiple functions.

My Solution

Click to show/hide solution


In my solution, I am getting the number of lines in my file with the getLines() function.  The getLines() function can receive a callback as an argument.  Below, I am declaring two more functions called logLines() and magicNumber().  The logLines() function prints the number of lines in my file to the console, and the magicNumber() function allows me to pass a magic number as the fourth argument in the command line.  If my file has as many lines or more than my magic number, it tells me me that it has more as many or more lines.  If not, the function says my file does not have as many or more lines.

Below my declared functions, I am passing logLines() and magicNumber() to the getLines() function.  When getLines() is done reading a file, it calls both of these functions so that they can do their job with the result of getLines().

Lesson 5

Contents

2 thoughts on “learnyounode Lesson 4 – My First Async I/O!

  1. Very helpful. Thank you! I was struggling on this one and the official solution seemed to be a minor case of callback hell as you pointed out.

    Is there also any way to move your doneReading function out of your getLines function? It’s not necessary, I’m just curious.

    • Thanks for the feedback!

      The doneReading function is actually the callback function to fs.readFile(). If it were moved outside of getLines, lines = fileContents.split('\n').length - 1 would error because fileContents would be undefined.

      I hadn’t thought about it, but typically callbacks are anonymous. My example is unconventional in that regard. If I were going to make a change, I think I would do something more like the example below.

      I think what you are getting at is that you would like to expose fileContents to another function or variable. This is possible. You can pass fileContents to the callback() in the getLines function.

      Check out the pack() method from a project I have been working on called boxVan.

      In the pack() method, I am passing the recordset from my sql query to the callback in the form on callback(recordset). Doing this allows me to pass the recordset to other methods. For example:

      I hope this helps!

Comments are closed.