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

299
Vistas
wait for the nested for loops

!I'm a beginner in js! is there any way to let the return wait for both for loops to finish I tried all the solutions here " https://stackoverflow.com/questions/27914209/how-could-i-run-a-function-after-the-completion-of-a-for-loop-in-node-js" but no one of them works I think because is nested loop


function StatisticsPerGameId (IDs){
    console.log(IDs)
    let doubldoubl={};
    for (let i=0; i <IDs.length;i++){
        const options = {
            method: 'GET',
            url: 'https://api-nba-v1.p.rapidapi.com/players/statistics',
            params: {game: IDs[i]},
            headers: {
              'X-RapidAPI-Host': 'api-nba-v1.p.rapidapi.com',
              'X-RapidAPI-Key': 'afgsdgbsdghbrynhndf6hn6dfhndfhn61dhddfhn'
            }
          };
          
          axios.request(options).then(function (response) {
            console.log(response.data.response[0].points)
            for(let j in response.data.response){
                let dd = 0;
                if( response.data.response[j].points >= 10){dd=dd+1;};
                if( response.data.response[j].totReb >= 10){dd=dd+1;};
                if(dd == 2 ){
                    doubldoubl=doubldoubl+{
                        name:response.data.response[j].player.firstname +" "+ response.data.response[j].player.lastname,
                        teamOfplayer : response.data.response[j].team.name,
                        teamOfplayerlogo:response.data.response[j].team.logo,
                    }
                };
                dd = 0 ;
            }
          }).catch(function (error) {
              console.error(error);
          }); 
    }
    return doubldoubl;
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

axios.request() returns a promise, which will resolve asynchronously when the request is fetched. Calling .then(function () {...}) will call that function when the request is ready.

The best way to wait for all the requests is to pass them all into Promise.all():

function StatisticsPerGameId (IDs){
    ...
    const promises = []
    for (let i=0; i <IDs.length;i++){
        ...
          const promise = axios.request(...).then(function (response) {...}).catch(function (error) {...}); 
          promises.push(promise)
    }
    return Promise.all(promises).then(function () {
        return doubldoubl;
    });
}

Note that this will make StatisticsPerGameId return a promise for the result, which will similarly need to be waited on asynchronously.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#composition

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