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

258
Vistas
How to execute code when foreach ends in javascript

When the code below runs I expect score to have a value (lets say: {"A": 1, "B": 2}), but when I print it I get an empty dict ({}).

I have tried to use promises but the result is the same.

driversBySeason(season) {
    var query = `SELECT raceId FROM races WHERE year = ${season}`;
    var score = {};

    this.con.query(query, (err, drop) => {
      if (err) {
        console.error(err);
      }

      drop.forEach((element) => {
        var raceId = element["raceId"];

        query = `SELECT driverId, points FROM results WHERE raceId = ${raceId}`;
        this.con.query(query, (err, drop) => {
          if (err) {
            console.error(err);
          }

          drop.forEach((element) => {
            if (score[element["driverId"]] == undefined) {
              score[element["driverId"]] = 0;
            } else if (score[element["points"]] != undefined) {
              score[element["driverId"]] += element["points"];
            }
          });
        });
      });
      console.log(score);
    });
  }
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

First of all you need to change your .forEach loop to for const of loop or just a regular for loop because it just fires the code inside the callback and never waits for it. And then you need to change your this.con.query(...) function which has callback to promises too. Your code should be like this:

const asyncQuery = (query) => new Promise((resolve, reject) => {
    this.con.query(query, (err, drop) => {
        if (!!err) {
            console.log(error);
            return reject(err)
        }
        return resolve(drop);
    })
});


async function driversBySeason(season) {
    var query = `SELECT raceId FROM races WHERE year = ${season}`;
    var score = {};
    const drop = await asyncQuery(query).catch(err => err /* some error handling */);
    for (const element of drop) {
        var raceId = element["raceId"];
        query = `SELECT driverId, points FROM results WHERE raceId = ${raceId}`;
        const drop2 = await asyncQuery(query).catch(err => err /* some error handling */);
        drop2.forEach((element) => {
            if (score[element["driverId"]] == undefined) {
                score[element["driverId"]] = 0;
            } else if (score[element["points"]] != undefined) {
                score[element["driverId"]] += element["points"];
            }
        });
        
    }
    console.log(score);
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You are outside of your callback. When this executes the code continues to run past "this.con.query". You are seeing your empty object that was assigned at the top do to this. Go inside the callback after the drop.forEach where you assign the values, or convert to an async/await approach.

about 4 years ago · Juan Pablo Isaza Denunciar

0

when you call driversBySeason your score variable has value of {}

and that is value for console.log() when you call it because of how closers are works and you updating score with callback function that happen later in time...

you get better understanding if you use promise...not callback

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