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

68
Visualizações
Chaining Async Await with loop

I have 2 functions with loops that I want to execute sequentially:

  • Function A executes

  • Within Function A there is a For loop triggering an external function

  • Once loop in Function A is finished doing its thing, then we start with Function B

I have tried with the following code but function B starts in parallel, doesn't wait for Function A to finish:

async function sendPosts() {
    console.log("start send posts")
    for (let post of posts) {
        ...
  
        await channel.send({
            ...
        }).then((message) => {
            externalFunction(message);
        })
        .catch(err => console.log(err));
    }
    console.log("finish send posts")    
}

async function sendComments() {
    console.log("start send comments")
    for (let comment of commentsData) {
        ... 
    }
    console.log("finish send comments")
}   

sendPosts().then(sendComments());
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

For of loop is not awaited. you looking for something as following

    for await (const variable of iterable) 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of

Foreach loop has acting the same

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