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

139
Visualizações
Async/await with multiple for loops

I am having a hard time wrapping my head around how aysnc/await works for my specific scenario. I have some for loops that I want to run parallel of each other but I also have other for loops that are dependent on the data create from one of the parallel for loops.

Loop 1 {
/* Delete some rows from a SQL Table */
}

Loop 2 {
/* Delete some rows from a SQL Table */
}

Loop 3 {
/* Add some rows to a SQL Table */
}

Loop 4 {
/* Update some rows from a SQL Table */
}

What I want to happen is Loop 1, Loop 2, and Loop 3 run in parallel instead of sequentially. I don't want Loop 4 to run until Loop 3 has finished. How could this be achieved with async/await. I am trying to improve runtime of this code but I need to use for loops to iterate through data in the database.

Alternatively, if there is a way to reduce the time complexity using a different method instead of for loops I am open to suggestions.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

await Promise.all(promiseLoop1, promiseLoop2, promiseLoop3);

await promiseLoop4;

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

Edit:

In addition, you can build the SQL command string using loops but only call SQL once.

You may also want to look into SQL Transactions:

https://docs.microsoft.com/en-us/sql/t-sql/language-elements/transactions-transact-sql?view=sql-server-ver15

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