Sunday, 14 May 2017

How to force close a readable stream?

I need force close a readline file... IN LINE 11! I need optimization!! But not workd the call this.emit("close");enter image description here

var readline = require('readline');
    var stream = require('stream');
    function get_line3(filename, number, callback) {
        var instream = fs.createReadStream(filename);
        var outstream = new stream;
        var rl = readline.createInterface(instream, outstream);
        var contp = 0;
        var data = "";

        rl.on("line", function(line) {
            console.log(contp);
            if (contp==number) this.emit("pause", line);
            contp++;        
        });

        rl.on("pause", function(line) {
            // do some work here
            console.log("doing some work");
            console.log(line);
            data = line;
            this.emit("resume");
        }); 

        rl.on("resume", function(line) { 
            this.emit("close"); // NOT WORK !!!
        });

        rl.on("close", function() {
            // do something on finish here
            callback(null, data);   
        });
    }



via Fábio Zangirolami

No comments:

Post a Comment