Reputation: 636
Am in a situation, where i have an array with thousands of items, first i break this array into groups of chunks 1000 per each, then i call a function which processes the first chunk in the array and it returns the new grouped chunks.
// Using lodash
var array = []; // my array with many items
var ids = _.chunk(array, 1000); // grouped array 1000 each
function LoopArrayGroups(ids){
var arrSize = _.size(ids); // number of groups
if(arrSize != 0){
// get First group array,remove it from the list
var toBeUsed = ids[0];
ids.splice(0, 1);
//process first group
myFunction(toBeUsed, function(promise){
if(!promise.error){
// return new array with the first group removed
return ids;
}else{
console.log("Some error " + promise.error);
}
});
}
}
The LoopArrayGroups method is called with an array of grouped chunks then it processes the first chunk, removes it then returns new grouped chunks ,
I just want to loop through the group arrays until they become empty. instead of calling this function over and over again
Upvotes: 1
Views: 1324
Reputation: 350310
Instead of:
return ids;
call the main function again:
LoopArrayGroups(ids);
The recursive call will increase the use of the stack on each call on most environments, because currently not many JavaScript engines have implemented the tail-recursion optimisation.
Practically this means that if you have a huge amount of chuncks, you could run into stack overflow errors. If this is a concern, you should alter the recursive call to make it asynchronous:
setTimeout(LoopArrayGroups.bind(ids), 0);
As you already have asynchronous processing in myFunction
, there is in fact no downside in doing it this way.
Upvotes: 4