About Piyas De

Piyas is Sun Microsystems certified Enterprise Architect with 10+ years of professional IT experience in various areas such as Architecture Definition, Define Enterprise Application, Client-server/e-business solutions.

Node.js Synchronous and Asynchronous Functions

In node.js platform, as the non-blocking programming model is followed as practice, all most all of the functions in the node.js modules are primarily asynchronous in nature. It means, the code block within the function will be mostly non-blocking to the end user and user will not ne prevented to perform different actions for various sub-processes.

Still we have synchronous counter part of the functions which are present in node.js modules.

Here is a typical example for  synchronous and asynchronous functions for the file system module for Node.js.

Asynchronous function for readfile -

var filesystem = require("fs");

filesystem.readFile("myfirstexample.txt", "utf8", function(error, data) {
  console.log(data);
});

In the above function file system module takes file name and pass the data of the file as reference to the callback handler of an anonymous function. Then this file system object will remain ready for performing any other file system operation handling task.

Whenever the full read of the file is complete, the callback function log the content of the file to the console. Here the application performance will not hamper in the system.

Synchronous function for readfile -

var filesystem  = require("fs");
var data = filesystem.readFileSync("myfirstexample.txt", "utf8");

console.log(data);

Here the function readFileSync completely read the contents from the file to the memory and then go to print the data in console. So the calling of the code block is synchronous here and without full content read, it will not go for the next line.

Now in a situation of active server, where most of the interaction will be done by end users, the second type of operation will impact the application performance as this is a blocking code model. Where as the first type of operation will give a more improved performance to the user because of the non-blocking code in nature.

We will discuss various other aspects of node.js programming model in future posts. So stay tuned and comment here with node.js feature discussion and useful links.
 

Related Whitepaper:

Software Architecture

This guide will introduce you to the world of Software Architecture!

This 162 page guide will cover topics within the field of software architecture including: software architecture as a solution balancing the concerns of different stakeholders, quality assurance, methods to describe and evaluate architectures, the influence of architecture on reuse, and the life cycle of a system and its architecture. This guide concludes with a comparison between the professions of software architect and software engineer.

Get it Now!  

Leave a Reply


six − 3 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books