I have such a code :
function onDoneFunction()
{
console.log("done!");
}
function generalQuery(generalArray, onDoneFunction)
{
function go(i)
{
if(i >= generalArray.length)
{
onDoneFunction();
}
else
{
iteratorFunction(generalArray[i], function()
{
console.log("entering callback " + i);
return go(i + 1);
});
}
}
go(0);
}
And my iteratorFunction
looks like this :
function iteratorFunction(partofquery, callback)
{
var index = generalArray.indexOf(partofquery);
collection.find(partofquery).then(function(data)
{
console("query completed " + index);
}
}
Supposing my query array has two elements, I see such outputs :
entering callback 0
entering callback 1
query completed 0
query completed 1
But I'm trying to see this :
entering callback 0
query completed 0
entering callback 1
query completed 1
I have been trying to make loop wait for an iteration to finish before starting the next one. I have tried many things. As you can see I have tried using recursion as well. But I can't achieve that. Could you please show me what I'm doing wrong? Thanks in advance.