Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!