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

156
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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