Using Q and Intentions to read from file with Node

web development


Using q is quite easy. In this article we are going to use the defer method to produce a deferred object comprising a worth that will be resolved or rejected later on. For instance, when we read from file with Node asynchronously, in the event the operation is successful, then you can resolve the item using the content of the file, or perhaps you can reject it and catch it within your code. Let’s gat started.

Produce a folder on your hard drive, navigate with it and begin a deal.json file and accept all of the defaults:

mkdir ~/Desktop/q-read && cd $_ && npm init

Install q:

npm i q -S # -S means like a production dependency

Create a dummy file:

touch dummy.txt
echo hello > dummy.txt

Make the main.js file which will support the main code:

touch main.js

Now we have been prepared to fill the main.js file.

First, let’s just browse the file with vanilla Node:

var fs = require(‘fs’);
fs.readFile(‘./dummy.txt’, ‘utf-8’, function (err, data)
if (err) return console.log(err);

Here i am just reading this content of the dummy.txt file in utf-8 format, and just logging this content to the console. In the event you run the code with node main.js, you should get the next output:

> node main.js

Now, let’s make things interesting. We’re going to develop a function called read that returns a promise:

var q = require(‘q’);
var read = function ()
var deferred = q.defer();
return deferred.promise;

Should you look at the code, first we load q. Then, we create a function and in the function we first produce a deferred object. So we just resolve it having a dummy value called ‘done’. At the end we return the promise property of deferred.

Now, let’s see the way we can use the read function. So, obviously we’re going to call the function first:


Once we call read it will return a promise. The promise includes a method called then which can be used to get into the resolved value:


Now the then method needs a function, and the resolved value will probably be passed towards the be the parameter:

read().then(function doSomething(resolvedData) );

And inside the function we are able to do everything we want using the data:

read().then(function doSomething(resolvedData)

This will log completed to the console, because that’s the value that we resolved. Now let’s examine the way we can use exactly the same principle to the fs.readFile method.

var fs = require(‘fs’);
var q = require(‘q’);
var read = function ()
var deferred = q.defer();
fs.readFile(‘./dummy.txt’, ‘utf-8’, function (err, data)
if (err) deferred.reject(err)
else deferred.resolve(data)
return deferred.promise;

read().then(function (content)
).catch(function (err)
).finally(function ()
console.log(‘Read is done.’);

In the event you look at the code, you can see that we’re utilizing the same principle. There is a handful of new things, why don’t we look at them.

Within the body of the function, we search for error, if an error happens we reject the promise with all the err. Otherwise, we resolved the promise using the content from the file. Now, whenever we call the read function, we could accept the resolved value within the then block. But if there is an error, we will catch that within the catch block. And also at the finish, it doesn’t matter what happened, we are going to log to the console within the finally block.

And that’s it! As an exercise, you can try implementing the fs.writeFile method also, the foundations are the same.
front-end development


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s