Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda