I was trying with this http://justinklemm.com/node-js-async-tutorial/
tutorial
I tried async.parallel() and I wanted to loop through some function again and again. But It was stuck onto first and didn't loop infinitely. Can anyone please suggest me regarding this?
EDIT : code : added 2 functions that should be lopped again and again
var util = require('util');
var async = require('async');
var SensorTag = require('./index');
var USE_READ = true;
SensorTag.discover(function(sensorTag) {
console.log('discovered: ' + sensorTag);
var items = [9000];
var asyncTasks = [];
items.forEach(function(item){
// We don't actually execute the async action here
// We add a function containing it to an array of "tasks"
asyncTasks.push(function(callback){
console.log('connectAndSetUp');
sensorTag.connectAndSetUp(callback);
}),
asyncTasks.push(function(callback){
console.log('readDeviceName');
sensorTag.readDeviceName(function(error, deviceName) {
console.log('\tdevice name = ' + deviceName);
callback();
});
});
});
asyncTasks.push(function(callback){
// Set a timeout for 3 seconds
setTimeout(function(){
// It's been 3 seconds, alert via callback
callback();
}, 3000);
});
//async.parallel(asyncTasks, function(){
//console.log('DONE');
async.parallel(asyncTasks, function(){
// All tasks are done now
//doSomethingOnceAllAreDone();
});
});
EDIT : I am trying with this one but not able to loop. Probably I am missing some fundamental issue here in node.js. Can anyone help me out?
var util = require('util');
var async = require('async');
var SensorTag = require('./index');
var USE_READ = true;
var count = 0;
/*PART 1*/
SensorTag.discover(function(sensorTag) {
console.log('discovered: ' + sensorTag);
async.series([
function(callback) {
console.log('connectAndSetUp');
sensorTag.connectAndSetUp(callback);
},
function(callback) {
console.log('readDeviceName');
sensorTag.readDeviceName(function(error, deviceName) {
console.log('\tdevice name = ' + deviceName);
callback();
});
},
function(callback) {
console.log('readSystemId');
sensorTag.readSystemId(function(error, systemId) {
console.log('\tsystem id = ' + systemId);
callback();
});
}
]);
});
/*PART 2*/
async.whilst(
function () { return count < 5; },
function (callback) {
function(callback) {
console.log('enableIrTemperature');
sensorTag.enableIrTemperature();
},
function(callback) {
setTimeout(callback, 2000);
},
function(callback) {
console.log('readIrTemperature');
sensorTag.readIrTemperature(function(error, objectTemperature, ambientTemperature) {
console.log('\tobject temperature = %d °C', objectTemperature.toFixed(1));
console.log('\tambient temperature = %d °C', ambientTemperature.toFixed(1));
callback();
});
},
function (err) {
console.log('5 seconds have passed');
// 5 seconds have passed
}
count++;
setTimeout(callback, 1000);
}
);
I can not loop through this. What I wanted is in the PART 1 I would execute the functions and then at the PART 2 I would loop through all function. I have tried recursive , may be not properly and unable to execute