So I have a function that makes a series of async calls to a MongoDB database. I want to return the result when all of the calls are done however the following code isn't doing that.
const getMatchesInfo = async mongoDBUserIds => {
_.map(mongoDBUserIds, async mongoDBUserId => {
const user = await UserCollection.findOne({
_id: mongoDBUserId
});
console.log('user.profile.name = ', user.profile.name);
return user;
});
};
I call this function in my Node.js API with:
module.exports = app => {
app.get('/api/matches', requireLogin, async (request, response) => {
const mongoDBUserIds = request.query.mongoDBUserIds.split(',');
let matches_info = await getMatchesInfo(mongoDBUserIds); // <=== await here
console.log('matches_info = ', matches_info);
response.send(matches_info);
});
};
What I don't understand is why is matches_info getting printed out before the console.logs inside function getMatchesInfo
? I thought the await here
was suppose to prevent code below it from running until getMatchesInfo
returned. What am I not understanding?