I'm feeling a bit lost on the best way to approach this task - hopefully, my explanation is clear.
In my forEach function, I am iterating through some JS object data and running a function for each item:
found.forEach(function(item) {
processData(item['userID']);
});
Inside the processData
function, I am making a MongoDB find() call.
var processData = function(userIDSelected) {
User.find({_id: userIDSelected},
{gender: 1, country:1}, function(req, foundUser) {
processUserInfo(foundUser[0]['gender']);
});
}
The challenge here is how to ensure that everything in the forEach loop completes before moving on, especially since each call triggers processUserInfo
one by one.
I've explored using the Q library and Q.all, but it doesn't seem to work as intended.
Is there a specific Q function that can help me wait for the entire chain of operations to finish?
Thank you