I have some trouble managing the flow control in node.js. Here is my problem:
I have an array of objects, and I want to do something with every object (for example write it to file or send somewhere via network...). I want to be sure that every object is processed and after that i wanna continue with some other function. In this example processObj() function is
iterating through every object and write that object to file. Where should I put callback call to be sure that callback function of processObj() will be executed after all objects are written to the file?
//Array of n objects...
var objArray=[{},{},{},{}];
//process every Object, for example write object to file...
function proccessObj(obj,callback){
var myObj = obj;
for(var i=0;i<myObj.length;i++){
//process single object, for example:
ws.appendFile('file.txt',myObj[i],function(){
//callback
})
}
callback() // <-- If i put callback here it will be executed before callbacks of ws.apendFile
}
//execute function
processObj(objArray,function{
//Do something after all objects in array are saved to file...
});