如何强制关闭可读流?

时间:2017-05-14 19:00:52

标签: node.js nodes

我需要强制关闭一个readline文件......在第11行!我需要优化!!但是没有处理调用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);   
        });
    }

0 个答案:

没有答案