async-write-to-stream.ts 279 B

123456789
  1. import type { Writable } from 'node:stream';
  2. import { once } from 'node:events';
  3. export const asyncWriteToStream = <T>(stream: Writable, chunk: T) => {
  4. const res = stream.write(chunk);
  5. if (!res) {
  6. return once(stream, 'drain'); // returns a promise only if needed
  7. }
  8. };