选择一种接口风格


Readable 流的 API 跨越多个 Node.js 版本的演进,并提供了多种消费流数据的方法。 一般情况下,开发者应该选择其中一种消费数据的方式,切忌使用多种方式消费单一流中的数据。 具体来说,使用 on('data')on('readable')pipe() 或异步迭代器的组合可能会导致不直观的行为。

建议大多数用户使用 readable.pipe() 方法,因为它已被实施以提供使用流数据的最简单方法。 需要对数据传输和生成进行更细粒度控制的开发者可以使用 EventEmitterreadable.on('readable')/readable.read()readable.pause()/readable.resume() API。

The Readable stream API evolved across multiple Node.js versions and provides multiple methods of consuming stream data. In general, developers should choose one of the methods of consuming data and should never use multiple methods to consume data from a single stream. Specifically, using a combination of on('data'), on('readable'), pipe(), or async iterators could lead to unintuitive behavior.

Use of the readable.pipe() method is recommended for most users as it has been implemented to provide the easiest way of consuming stream data. Developers that require more fine-grained control over the transfer and generation of data can use the EventEmitter and readable.on('readable')/readable.read() or the readable.pause()/readable.resume() APIs.