Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

136
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda