Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

73
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda