nickewing / line-reader

Asynchronous line-by-line file reader for node.js
MIT License
453 stars 75 forks source link

Calling async function during line processing #2

Closed jochenonline closed 12 years ago

jochenonline commented 12 years ago

Hello!

Very nice package, really! How can I call an async funtion (i.e. writing the line data to a database) during line processing? The problem for me is, that I want to call nextLine() in the callback of my async function. This does not work, because code is running forth in the meantime.

Thanks for your help.

nickewing commented 12 years ago

Hello Jochen,

I see the problem and you're right, the eachLine function did not really make this possible. I've updated the function to take a callback as the third paramater. I think it should work for what you're trying to do.

Example:

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

lineReader.eachLine('file.txt', function(line, last, cb) {
  console.log(line);

  if (/* done */) {
    cb(false); // stop reading
  } else {
    cb();
  }
});

Thanks for pointing that out!