Consider the code below ... I am trying to pause the stream after reading the first 5 lines:
var fs = require(\'fs\');
var readline = require(\'r
Somewhat unintuitively, the pause methods does not stop queued up line events:
Calling
rl.pause()
does not immediately pause other events (including'line'
) from being emitted by thereadline.Interface
instance.
There is however a 3rd-party module named line-by-line where pause
does pause the line
events until it is resumed.
var LineByLineReader = require('line-by-line'), lr = new LineByLineReader('big_file.txt'); lr.on('error', function (err) { // 'err' contains error object }); lr.on('line', function (line) { // pause emitting of lines... lr.pause(); // ...do your asynchronous line processing.. setTimeout(function () { // ...and continue emitting lines. lr.resume(); }, 100); }); lr.on('end', function () { // All lines are read, file is closed now. });
(I have no affiliation with the module, just found it useful for dealing with this issue.)