Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
Async/await, Promises and .map()

I'm building backend to fetch pictures of users I follow. The function below is a helper function that takes an array of user IDs to map against it to get their pictures. This function returns a single promise.

const getFeedPhotos = async (followingArr) => {        
  const promises = followingArr.map(async userId => { 
      await Photo.find({userId: mongoose.Types.ObjectId(userId)});
  });
  const x = Promise.all(promises);
  return x;
};

Code below is the router itself where I am invoking my helper function.

photoRouter.get(
    '/feed',
    expressAsyncHandler(async (req, res) => {
      const following = req.body.following;

      if (following) {
        const response = getFeedPhotos(following);
        console.log(response);
      } else {
          res.status(400).send({ message: '"Following" is not provided.' })
      }

    })
  );

The problem is that response equals to Promise { <pending> }. I need this promise to resolve in order to send it to my client. My question is how do I do that? I do realize that it has to do with some blunder of mine and stems from my misunderstanding of the whole async concept per se. I was trying to make some research but none of the solutions worked for me.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda