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

124
Visualizações
wait for finish function

I've this function with map, how can call only when finish?

 _.map(data,async function (el) {
        if(Number.parseInt(el) != NaN && (el != null) && !isNaN(el)){
            console.log('ID: ' + el)
            await scrapeDetails(el) // <--- need to wait finish and than go to next element in map
        }
    })

async function scrapeDetails(id){
// ... bla bla
}
about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

map and other higher order functions have funky behaviors when given async...await functions. The best solution I found so far is using the for...of syntax with await. Something like this:

for (const element of data) {
  // do all operations here
}
about 4 years ago · Santiago Gelvez Relatório

0

Collect the promises and resolve them with Promise.all(). They're all done when that's done.

const promises = _.map(data, function (el) {
    if(Number.parseInt(el) != NaN && (el != null) && !isNaN(el)){
        console.log('ID: ' + el)
        return scrapeDetails(el)
    } else {
        return Promise.resolve();
    }
});

let allResults = await Promise.all(promises);
console.log('now we are done!')
about 4 years ago · Santiago Gelvez Relatório

0

Or you can always use recursion.

const asyncProcess = async (array, index) => {
  const value = array[index];
  if (!value) return;
  ..
  await scrapeDetails(el);
  return asyncProcess(array, index + 1);
}
about 4 years ago · Santiago Gelvez 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