I am aware that I used "wait" and "asyncronous" word in the same sentense which doesn't make sense. But I would like to know if someone can suggest me a solution to my problem.
I have for loop that runs on an array of docs returned by mongodb. Now, depending on some logic I need to remove certain document. Only after I am finished removing the docs, I can call my next method. Looks something like following:
mymongodbconnection.find({}).toArray(function(err, documents) {
if (!err && documents.length !== 0) {
documents.forEach(function(document) {
if( someCheckPerformed(document)){
console.log('keeping');
}else{
console.log('removing');
mymongodbconnection.remove({_id:document._id},fun(err,result){});
}
});
notifyAdminAboutChange();
} else {
logger.warn('No existing UA docs to filter');
}
});
Now, as you can see, I need to call notifyAdminAboutChange(); only after for loop has finished iterating over all the docs and has removed docs that needs to be removed.
My questions:
- Can I block the for loop execution to wait for removal to finish?
- If 1 is not possible then how do I achieve this use case?