I am fetching userData from MongoDB by passing userId then awaiting for userData. Its returning userData if I request data without an array of ids. it gives me data. but if I run a loop on Array of ids and await that It gives me an array of promises but not an array of user data. why is that happening?.
I know how to solve it by await Promise.all(promiseArray).
but my question is why is this behavior? and also when I try to console the user in the map function it consoles userData, not the promise (on line 156). and I have learned that await stop the execution wait for the line that is awaiting to run then after that i continues. but here its not the case. its not stopping the map function instead if doesn't run code after the await but showing the behavior of continue keyword at await. and then after when the promise of await if fulfilled then code after that runs.
here is the code
// this.guides = ["userIdOne","userIdTwo"]
tourSchema.post("save", async function (doc, next) {
const guideSingle = await User.findById(this.guides[0]);
const guideArray = this.guides.map(async (id) => {
console.log("ID: ", id);
let user = await User.findById(id);
console.log("USER: ", user);
return user;
});
console.log("SINGLE GUIDE: ", guideSingle);
console.log("ARRAY GUIDE:", guideArray);
next();
});