Sunday, July 24, 2016

NodeJS Series: Calling Sleep Method.

Dear Friends,

This is my first article about NodeJS, I am currently exploring it and problems which I found during my learning sessions, I will share it to you with my blog posts.

Today while writing some code, I come across the need of sleep functionality between two transactions. While doing google, I found "sleep" library which provide required functionality.

Calling of sleep method is simple e.g.,

sleep.sleep(1) // time will be defined in seconds 

But before that you need sleep library / package. To download it, use following npm command

npm install sleep

After successful installation, write following code to test this functionality.

var sleep = require('sleep');

function callLoops()
{
for(var a=1;a<10;a++)
{
console.log('%d',a);
sleep.sleep(1); //times in seconds.
}
}

callLoops();

and save this code in myExample.js file.

Execute the file with following command

node myExample.js

Congratulations...... You have achieved the goal.

That's all for now folks..... If this post help you then please increase its credibility by posting your comments.

Thanks.

No comments: