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

229
Views
JavaScript variable scope misunderstanding. The content of the array is empty in another scope, even though the variable is global

I'm trying to read a file line by line using this:

function makeList() {
  let lines = [];
  var lineReader = require('readline').createInterface({
    input: require('fs').createReadStream('titles.txt')
  });
  lineReader.on('line', function(line) {
    //console.log(typeof line)
    lines.push(line);
    //console.log(lines)

  });
  return lines;
}

When enabling the comments I can see that the lines are read and are strings.

But when calling the function I always get an empty list:

async function run() {
  var p = makeList();
  console.log(p);
  for (line of lines) {
    console.log("Elaborando: " + line)
    for (let i = 1; i <= 18; i++) {

      if (!processString(line)) {

        fs.mkdirSync(processString(line));
      }
      await sleep(5000);
      //getImage(line, i, line);

    }
  }
}

run()

printing p to console gives an empty list. I'm not familiar with JavaScript, what am I doing wrong?

about 4 years ago · Juan Pablo Isaza
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!