I am using this forEach loop to push some data into array and send that to client side
var arrayToSend = [] //Empty Array
SomeArray.forEach(function(item){
Collection.findById(item.id,function (err,data) {
if (err) {
console.log(err)
} else {
arrayToSend.push(data.record)
}
//How can I run this next line at end of loop asynchronously
res.render("index",{arrayToSend : arrayToSend })
})
})
}
How can I use async to run res.render("index",{arrayToSend : arrayToSend }) after an loop end within forEach scope in form of callback,otherwise if I run this outside the loop it will send empty array ?