Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
当我在节点中实现可写流时,我必须实现的唯一方法是_write. 但是我怎么知道不会有更多的数据呢?
_write
我的流的消费者以 结束他的传输,并且当我完成写入时mystream.end(data)他也会收到一个事件。finish但是我如何在我的可写实现中知道这一点?
mystream.end(data)
finish
writable.end()好吧,一个可写对象在被调用时会发出一个“完成”事件。因此,添加一个finish事件侦听器,然后在可写结束时调用处理程序。
writable.end()
class MyWritable extends Stream.Writable { constructor() { super() this.on('finish', function() { console.log('no more data to be written') } } }
参考:https ://nodejs.org/api/stream.html#stream_event_finish