One way is to use the async library, in particular the async.each or async.eachSeries
function. (You could use async.map
, but in your case you are actually not mapping, but modifying the underlying array items directly.)
In particular, your code would look like this:
async.each(res.Data, function(val,callback){
getdetails(val.ID).then(function(data){
tmp = JSON.parse(data);
Object.keys(tmp.Data[0]).map(function(v,j){
val[v] = tmp.Data[0][v];
});
callback(null); // Asynchronous code has finished without error
}, function(error){ //error callback
callback(error); // Asynchronous code has finished with error
});
}, function(error) {
// All asynchronous calls have finished
if(error)
console.log("Error", error);
else
console.log("Success", res);
});
async.each
will run many iterations at the same time, while async.eachSeries
will run only one iteration at the same time.