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

212
Views
Node.js: Any way to stop reading the file and the whole process in line-reader's eachLine function?

I have this code:

const lineReader = require('line-reader');

var truevar = false;

async function readLines(filename, processLine) {
    return new Promise((resolve, reject) => {
        lineReader.eachLine(filename, (line, last, callback) => { 
           if (!callback) throw new Error('panic');
               if (truevar) {
                   // I wanna stop/finish the function here
               } else {
                   processLine(line)
                   .then(() => last ? resolve() : callback())
                   .catch(reject);
               };
           });
       })
   };

And here's how I call the readLines function:

await readLines('somefile.txt', async (line) => {
    await delay(1000) // 1 second
    other_function(line);
});

I need to stop reading the file & finish the entire process once it gets to a certain point. I've read the line-reader docs, but found nothing. I also tried to do it the dirty way with "break;" etc but it's obviously just not gonna finish the job and be stuck (can't call it again unless I reload the program) so that's useless.

EDIT: Fixed the problem. I forgot to set the truevar back to false, in order to be able to call the function again.

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

0

Try this:

lineReader.eachLine(filename, (line, last, callback) => { 
  if (!callback) throw new Error('panic');
  if (truevar) {

    // Add to stop reading
    cb(false);        

  } else {
    processLine(line)
      .then(() => last ? resolve() : callback())
      .catch(reject);
  };
});

Source: https://www.npmjs.com/package/line-reader

about 4 years ago · Juan Pablo Isaza Report

0

Found out why I wasn't able to call the function again. It wasn't the issue with async function not being stopped properly, I just didn't set the truevar back to false, in order to be able to call the function again.

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!