How to close a readable stream (before end)?

前端 未结 9 1403
名媛妹妹
名媛妹妹 2020-11-29 03:16

How to close a readable stream in Node.js?

var input = fs.createReadStream(\'lines.txt\');

input.on(\'data\', function(data) {
   // after closing the strea         


        
9条回答
  •  旧巷少年郎
    2020-11-29 03:40

    You can clear and close the stream with yourstream.resume(), which will dump everything on the stream and eventually close it.

    From the official docs:

    readable.resume():

    Return: this

    This method will cause the readable stream to resume emitting 'data' events.

    This method will switch the stream into flowing mode. If you do not want to consume the data from a stream, but you do want to get to its 'end' event, you can call stream.resume() to open the flow of data.

    var readable = getReadableStreamSomehow();
    readable.resume();
    readable.on('end', () => {
      console.log('got to the end, but did not read anything');
    });
    

提交回复
热议问题