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


  • ...transforms <Function> | <Object> 可选转换应用于可读侧。
  • options <Object>
    • budget <number> 在施加反压之前,缓冲的最大字节数。必须 >= 16384。默认值: 16384。
    • backpressure <string> 背压策略:'strict'、'unbounded'、'drop-oldest' 或 'drop-newest'。默认值:'strict'。
    • signal <AbortSignal> 中止流。信号在缓冲数据从 writer.end() 排出时仍然保持活跃,在此期间中止会导致写入失败,并拒绝待处理的 end() promise。
  • 返回:<Object>

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

🌐 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][].