Everytime I send back timelinePosts I get undefined. I understand that the forEach finishes faster than the promise executes but how can I fix this? I have tried putting a function inside the forEach and have it execute the second promise but it doesn't work.
getTimelinePosts: (req, res, next) => {
const db = req.app.get("db");
const { userID } = req.params;
let timelinePosts = [];
db.getFollowing([userID]).then(friends => {
friends.forEach((val, i, arr) => {
db.getTimelinePosts([val.friend_id]).then(post => {
timelinePosts.push(post);
});
});
res.status(200).send(timelinePosts);
});
}