Why is my function returning Promise { } [duplicate]

const postCount = boards.map(async (boardI) => {
  const posts = await Post.find({ board: boardI.slug });
  return [boardI.slug, posts.length];
});

Since this is an async function, it will return a promise. map calls the function for each element of the array, gets the promises they return, and creates a new array containing those promises.

If you would like to wait for that array of promises to each finish, use Promise.all to combine them into a single promise, and then await the result.

const promises = boards.map(async (boardI) => {
  const posts = await Post.find({ board: boardI.slug });
  return [boardI.slug, posts.length];
});
const postCount = await Promise.all(promises);
console.log(postCount);

Leave a Comment