Share this content on Facebook!
09 May 2016

web development


Using q is quite easy. In this article we're going to utilize the defer strategy to create a deferred object comprising a worth that'll be resolved or rejected in the future. As an example, once we read from file with Node asynchronously, in the event the operation is successful, then you can resolve the item using the content with the file, or else you can reject it and catch it within your code. Let's gat started.


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


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


Install q:


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


Produce a dummy file:


touch dummy.txt 

echo hello > dummy.txt 


Make the main.js file that may contain the main code:


touch main.js 


Now we're prepared to fill the key.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);

 console.log(data);

);


The actual just reading the information with the dummy.txt file in utf-8 format, and just logging the information towards the console. If you run the code with node main.js, you ought to get these output:


> node main.js 

hello 


Now, let's make things interesting. We will develop a function called read that returns a promise:


var q = require('q'); 

var read = function () 

 var deferred = q.defer();

 deferred.resolve('done');

 return deferred.promise;

;


Should you go through the code, first we load q. Then, we create a function and in the part we first create a deferred object. And we just resolve it with a dummy value called 'done'. And at the end we return the promise property of deferred.


Now, let's see the way you may use the read function. So, obviously we're going to call the part first:


read() 


Whenever we call see clearly will return a promise. The promise features a method called then which you can use to get into the resolved value:


read().then(); 


The actual then method needs a function, as well as the resolved value will probably be passed for the be the parameter:


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


And inside the function we could do everything we want with all the data:


read().then(function doSomething(resolvedData) 

 console.log(resolvedData);

);


This may log implemented to the console, because this is the value that individuals resolved. Now let's wait and watch the way you can apply exactly the same principle towards 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) 

 console.log(content);

).catch(function (err) 

 console.log(err);

).finally(function () 

 console.log('Read is done.');

);


If you look at the code, we're employing the same principle. There are a number of something totally new, so let's review them.


Within the body from the function, we check for error, if an error happens we reject the promise with the err. Otherwise, we resolved the promise using the content with the file. Now, whenever we call the read function, we can grab the resolved value in the then block. But when it comes with an error, we'll catch that inside the catch block. At the finish, it doesn't matter what happened, we are going to log for the console in the finally block.


Which is it! As an exercise, you can try implementing the fs.writeFile method as well, the foundations are the same.

front-end development



Comments

There isn't any comment in this page yet!

Do you want to be the first commenter?


New Comment

Full Name:
E-Mail Address:
Your website (if exists):
Your Comment:
Security code: