Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!