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.
 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


six + = 8



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
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.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close