My Scenario:
Inside a forEach loop i executed a function with callback as like below:
var rows = [];
rows.forEach(function(elem, i){
///done some stuff
client.search(query).then(function(facResult){
//done some stuff
});
});
My problem is for first iteration the "client.search(query)" is executed before finishing this stuff the second iteration is started because client.search(query)
is executed with callback. I have to push first all result into an array.
So before i am getting first result the second is appended, sometimes i am not getting even the first result.
What i have to do to execute the for loop in order. I searched a lot , some suggestions is to use timedelay
, but i don't need that. Help me to solve this. Thanks in advance.