Search code examples
javascriptnode.jsfilewaitsequential

Execute function sequentially for each line of file in nodeJS


First of all, I've found similar questions but nothing quite matching my use case:

I have a file such as:

line1prop1 line1prop2 line1prop3
line2prop1 line2prop2 line2prop3
line3prop1 line3prop2 line3prop3
...

I want to read the file line by line, execute a function for each line BUT wait for the function to finish before moving to the next line

This is because for each line I have to extract the properties and make a request to Elasticsearch, to see if I have a matching document or not, but my cluster gets flooded with requests because I currently read everything asynchronously.

    var lineReader = require('readline').createInterface({
  input: require('fs').createReadStream('file.in')
});

lineReader.on('line', function (line) {
  //EXECUTE FUNCTION HERE AND WAIT FOR IT TO FINISH
});

Any help is greatly appreciated!


Solution

  • You need to understand the when you use .on listeners, you are listening to a event emitted

    The 'line' event is emitted whenever the input stream receives an end-of-line input (\n, \r, or \r\n). This usually occurs when the user presses Enter or Return.

    The listener function is called with a string containing the single line of received input.

    Since it's a listener, you cannot control how event is being emitted.

    line.pause() won't work here either because of the following

    Calling rl.pause() does not immediately pause other events (including 'line') from being emitted by the readline.Interface instance.

    If you want to, you should probably read the entire the file in to a javascript array, and use for loops to go through each line.

    node.js: read a text file into an array. (Each line an item in the array.)