In Node.js I have a function that goes through an entire list of elements and does some database calls as shown below:
for(var j=0; j<elements.length; j++)
{
//do some database calls
}
After each loop finishes (meaning when the database calls terminate), I would like to "console.log("Loop terminated");". In the code below I have shown my attempt to solve this:
for(var j=0; j<elements.length; j++)
{
(function()
{
//do some database calls
})(function(){
console.log("Loop terminated");
});
}
I use an anonymous function in place, and I am trying to callback a function that will print "console.log("Loop terminated")". When I execute this code, the console.log never prints anything. I am pretty new to Node.js and I do not understand callbacks that well. Can someone explain to me why my callback is not working and how can I fix it?