Why Does My Async Function Return An Empty Array
Solution 1:
It's because .map()
is not async
aware. It doesn't wait for the promise that the callback returns. So, when you do this:
await userMatchs.map(...)
The .map()
returns an array. You are calling await on an array of promises (remember, .map()
returns an array). That doesn't do anything useful. It doesn't wait for anything and the individual iterations inside the .map()
didn't wait either.
You can either switch to plain for
loop because a for
loop is promise aware and it will await
properly or you can use await Promise.all(userMatchs.map(...))
.
You could do this:
function getMatchsByUser(user) {
return Promise.all(user.matchs.map((m) => {
return Match.findById(m._id).select([
"-isConfirmed",
"-isUnmatched",
]));
});
}
Or, if you want to do the requests to your database sequentially one at a time, use a plain for
loop which await
will work in:
async function getMatchsByUser(user) {
let matchs = [];
for (let m of user.matchs) {
let match = await Match.findById(m._id).select([
"-isConfirmed",
"-isUnmatched",
]);
matchs.push(match);
}
return matchs;
}
Solution 2:
This code
userMatchs.map(async (m) => {
let match = await Match.findById(m._id).select([
"-isConfirmed",
"-isUnmatched",
]);
matchs.push(match);
}); // -> return an array of promises
It returns an array of promises, so await
will not wait for that to execute, -> return empty array.
For this, you should use Promise.all()
Something like this:
async function getMatchsByUser(user) {
const userMatchs = user.matchs;
let matches = await Promise.all(userMatchs.map(async (m) => {
return Match.findById(m._id).select([
"-isConfirmed",
"-isUnmatched",
]);
}));
return matches;
}
Solution 3:
Your problem is userMatchs.map
Using async/await combined with map() can be a little tricky
Post a Comment for "Why Does My Async Function Return An Empty Array"