Hi I'm trying to create a synchronous loop using underscore js. For each loop iteration I make some further asynchronous calls. However, I need to wait until each iteration call is finished before I move on to the next iteration.
Is this possible in underscore js ? If yes, how so ? could someone please provide an example ?
_.( items ).each( function(item) {
// make aync call and wait till done
processItem(item, function callBack(data, err){
// success. ready to move to the next item.
});
// need to wait till processItem is done.
});
UPDATE I solved this using the async.eachSeries method.
async.eachSeries( items, function( item, callback){
processItem(item, function callBack(data, err){
// Do the processing....
// success. ready to move to the next item.
callback(); // the callback is used to flag success
// andgo back to the next iteration
});
});