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

139
Views
JavaScript logging done before loop is finished

I want to log some data coming from a CSV file using a for loop for each line. When the for loop is done, I want it to log "done", but it seems like it logs the "done" before the last line of det csv file... CSV File:

CHANNEL,TITLE,TEXT
EzaguesTesting,TITLE TIL SR 1,TEKST TIL SR 1
EzaguesTesting2,TITLE TIL SR 2,TEKST TIL SR 2
EzaguesTesting3,TITLE TIL SR 3,TEKST TIL SR 3
EzaguesTesting4,TITLE TIL SR 4,TEKST TIL SR 4
EzaguesTesting5,TITLE TIL SR 5,TEKST TIL SR 5

The code that should log it:

fs.readdir(dirPath, async function (err, lines) {
    for await (const line of lines) {
        await new Promise(resolve => {
            fs.createReadStream(dirPath + line)
                .pipe(csv())
                .on('data', data => {
                    console.log('SR: ' + data.CHANNEL + ' Titel: ' + data.TITLE + ' Tekst: ' + data.TEXT);
                    resolve(true);
                });
        })
    }
    console.log('done');
})

Everything seems to work fine, other than the fact that it logs "done" one line too early...

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You resolve your Promise on 'data'. It means that, as soon as your stream sends you the first chunk, you resolve the Promise with this chunk only.

You need to send the result back .on('end').

const files = await fs.promises.readdir(dirPath);

for (const file of files) {
    await new Promise(resolve => {

        let result 

        fs.createReadStream(dirPath + line)
            .pipe(csv())
            .on('data', data => {
                result = data;
            })
            .on('end', () => resolve(result));
    })
}
console.log('done');

I have made some other changes, like using the Promise version of fs.readdir and also renaming lines to files (you are getting file names, not lines).

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!