I am sure there is a simple answer to this. I just can't seem to get my head fully around Promises.
From everything I have reading, the below code should be behaving as I expect.
From what I can gather, Post.findById should await, then the following console.log('A') should execute before console.log('B'). However that is not happening. console.log('B') is executing before console.log('A').
It appears the await in let parent_post = await Post.findById
is having no effect, and console.log('B') is running before console.log('B').
Any suggestions on what I may be doing wrong with this?
exports.save = async function(req, res) {
let parent_post = await Post.findById(req.body.comment_of)
.populate("starred_users", "name email")
.exec(function(err,parent_post){
console.log('A');
console.log(parent_post);
return parent_post;
});
console.log('B');
console.log(parent_post);
// I need to do stuff to parent_post here before returning
return parent_post
}
Thanks in advance.