I am new to Nodejs and need some guidance for writing better code. Here is my problem.
I have a function which is which I am using async water fall model. I want to call that function in a loop and break the loop if some thing goes wrong in the middle other wise notify some result at the end of for loop. But for some reason I am getting an undefined response.
function myFunc (arg1) {
async.waterfall(
[
function() {
//do something
callback(null, data);
},
function(data, callback) {
//do something
callback(null, 'done');
}
],
function(err, result) {
return {"error" : err, "res" : result};
}
}
//Here I am calling my function
for (var d in mydata) {
var retdata = myFunc (mydata[d]); //retdata has undefined in it as this line executes before the last function of water fall
if (retdata.error !== 200) {
return // break loop
}
}
//Other wise if every thing is fine nofify after the for loop end
console.log ("done");
In short what is the correct and best way of notify the result (if true) at the end or break loop when last function of waterfall gives error.