I want to minimize following code. Working in node js
var each = 0;
var final = {};
// myLoop have some array values;
myLoop.forEach(function(row) {
//data retrive from db
db.query('SELECT * FROM test where type=?',[row.type], function(err, result) {
final[each] = result;
each++;
if (each == myLoop.length) {
return final;
}
});
});
Above code working fine, but trying to avoid if (each == myLoop.length) condition. Is anything available to identify the loop is completed?
Is possible something like below:
myLoop.forEach(function(row) {
//do needs
}).done({
return final;
});