ondrain(drainable)
等待可排水写入器的背压清除。如果对象未实现可排水协议,则返回 null,或者返回一个在写入器可以接受更多数据时满足 true 的承诺。
🌐 Wait for a drainable writer's backpressure to clear. Returns null if
the object does not implement the drainable protocol, or a promise that
fulfills with true when the writer can accept more data.
import { push, ondrain, text } from 'node:stream/iter';
const { writer, readable } = push({ budget: 16384 });
const chunk = new Uint8Array(8192); // 8 KB
writer.writeSync(chunk);
writer.writeSync(chunk); // 16 KB total -- buffer full
// Start consuming so the buffer can actually drain
const consuming = text(readable);
// Buffer is full -- wait for drain
const canWrite = await ondrain(writer);
if (canWrite) {
await writer.write('c');
}
await writer.end();
await consuming;const { push, ondrain, text } = require('node:stream/iter');
async function run() {
const { writer, readable } = push({ budget: 16384 });
const chunk = new Uint8Array(8192); // 8 KB
writer.writeSync(chunk);
writer.writeSync(chunk); // 16 KB total -- buffer full
// Start consuming so the buffer can actually drain
const consuming = text(readable);
// Buffer is full -- wait for drain
const canWrite = await ondrain(writer);
if (canWrite) {
await writer.write('c');
}
await writer.end();
await consuming;
}
run().catch(console.error);