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

154
Views
How to await promise function inside nested maps?

Suppose I have the following:

async function getReactionScores(userReactions) {
  const reactionsWithSharesPromises = userReactions.map((reactions) => {
    return {
      emoji: reactions.emoji,
      shares: reactions.users.map(async (user) => {
        return {
          username: user.username,
          // Just as an example of something to wait for
          shares: await sleep(1, user.id),
        };
      }),
    };
  });

}

function sleep(time, userID) {
  return new Promise((resolve) => setTimeout(resolve(userID), time));
}

How do I await for all the promises inside the second map to resolve? If it was only one map, I could do Promise.all, but promise all is not recursive.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You will need 2 Promise.all

async function getReactionScores(userReactions) {
  const reactionsWithSharesPromises = await Promise.all(userReactions.map(async (reactions) => {
    return {
      emoji: reactions.emoji,
      shares: await Promise.all(reactions.users.map(async (user) => {
        return {
          username: user.username,
          // Just as an example of something to wait for
          shares: await sleep(1, user.id),
        };
      })),
    };
  }));
}
about 4 years ago · Juan Pablo Isaza Report
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!