Tuesday, 18 April 2017

How to read mutiple files with readl-async node module

I am trying to read multiple files line by line using the 'readl-async' package. I was able to do it for a single file, but when I try to run it in a loop only the last file is processed.

Here is my code:

    var alphabet = "abc".split("");
    alphabet.forEach(function (alpha) {
            var fileName = alpha + ".txt";
            var reader = new readl(fileName, {
                encoding: 'utf8'
            });

            //Emit this function when one line is read: 
            reader.on('line', function (line, index, start, end) {
                // .... 
            });

            //Emit this function when the file is full read 
            reader.on('end', function () {
                // .... 
                fs.writeFile(fileName + ".json", JSON.stringify(jsonObj), function (err) {
                    if (err) return console.log(err);
                });
            });

            //Emit this function when an error occurs 
            reader.on('error', function (error) {
                // .... 
            });

            reader.read();
        }

When I run it I only get the file c.json and not all. What changes can I make to execute it correctly for multiple files?



via Gaurav N

No comments:

Post a Comment