Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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);
    });
  }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading