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 our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

Leave a Reply


+ 9 = ten



Java Code Geeks and all content copyright © 2010-2015, 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 our best selling eBooks for FREE!

Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close