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

215
Vistas
How do I handle promises inside loop inside async function in javascript?
async func(){
  try{
    let iter=[1,2,3]
    for (i of iter){
      let result=await someFunc()
      pool.query("some query",arrWithResult)
    }
  }catch(err){console.log(err)}
}

Here, the loop waits for await. But I don't want that. I need the result and sql query to be executed in sequence but the loop should continue without waiting. I also need value of current i when executing query. Otherwise, I would just push these promises into array and do Promise.all(arr)

async func(){
  try{
    let iter=[1,2,3]
    for (i of iter){
      someFunc().then(body=>{pool.query("some query",arrWithResult).catch(err=>err)
    }
  }catch(err){console.log(err)}
}

Is this okay? I also thought of making then part async but should I add another try catch block?

So, what is best way for this? Or should i go for then/catch. When doing Promise.all, the value of iter gets lost.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Maybe I'm underthinking it (or you're overthinking it), but why would you not just refactor into another async function that does what needs to be done on each iteration of the loop, and then Promise.all() the list of returned promises to let them resolve?

Something like this:

async func f1() {
 const iter = [ 1, 2, 3 ] ;
 return await Promise.all( iter.map(f2) );
}

async func f2(i) {
 const result = await someFunc() ;
 return await pool.query( "some query" , arrWithResult );
}

The the caller then just needs to say ...

const responses = await f1();
```
about 4 years ago · Juan Pablo Isaza Denunciar

0

Don't use an iterator var for this - this is a job for Array.map()

Promise.all([1,2,3].map(i => someFunc().then(body => pool.query("some query",arrWithResult).catch(err=>err));
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