• Empleos
  • Sobre nosotros
  • Empleos
    • Inicio
    • Empleos
    • Cursos y retos
  • Empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

104
Vistas
while loop not waiting for await

I know there's a ton of these questions but I haven't been able to find one that fits my case. I'm making api calls in a while loop and just don't want to time out my calls by rapid firing them. The while loop doesn't seem to be waiting for the call to finish.

while(it < array.length){
    await apiFunction(array[it]);
    console.log(it);
    it++
}

and the function

async apiFunction(array){
    try{
        (async () => {
            const result = await api.doStuff(array);
            console.dir(result);
        }
    } catch (e) {
        stuff;
    }
}

I've also tried adding a return(result) but that didn't change anything

My console output is

0
1
result
result

How can I get the while loop to wait until the api call is done? so my console looks like

0
result
1
result
over 3 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I had too many layers of asynchronicity going I guess. Just removed the async from inside the apiFunction

async apiFunction(array){
    try{
        const result = await api.doStuff(array);
        console.dir(result);
    } catch (e) {
        stuff;
    }
}
over 3 years ago · Santiago Trujillo 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda