push([...transforms][, options])


  • ...transforms <Function> | <Object> 可选转换应用于可读侧。
  • options <Object>
    • highWaterMark <number> 在应用回压之前的最大缓冲槽数。必须 >= 1;小于 1 的值将被限制为 1。默认值: 4
    • backpressure <string> 背压策略:'strict''block''drop-oldest''drop-newest'默认:'strict'
    • signal <AbortSignal> 中止流。
  • 返回:<Object>
    • writer PushWriter 作家的方面。
    • readable AsyncIterable 可读的一面。

创建一个具有背压的推流。写入器推送数据;可读端作为异步可迭代对象被消费。

🌐 Create a push stream with backpressure. The writer pushes data in; the readable side is consumed as an async iterable.

import { push, text } from 'node:stream/iter';

const { writer, readable } = push();

// Producer and consumer must run concurrently. With strict backpressure
// (the default), awaited writes block until the consumer reads.
const producing = (async () => {
  await writer.write('hello');
  await writer.write(' world');
  await writer.end();
})();

console.log(await text(readable)); // 'hello world'
await producing;const { push, text } = require('node:stream/iter');

async function run() {
  const { writer, readable } = push();

  // Producer and consumer must run concurrently. With strict backpressure
  // (the default), awaited writes block until the consumer reads.
  const producing = (async () => {
    await writer.write('hello');
    await writer.write(' world');
    await writer.end();
  })();

  console.log(await text(readable)); // 'hello world'
  await producing;
}

run().catch(console.error);

写入器返回 push() 符合 [Writer 接口][]。

🌐 The writer returned by push() conforms to the [Writer interface][].