Dev Helpers
← Back

Count chunks in a writable sink

Count chunks in a writable sink using Node.js.

Open this example in the interactive library

Source code

const source = () =>
  new ReadableStream({
    start(c) {
      c.enqueue("a");
      c.enqueue("b");
      c.close();
    },
  });
console.log(
  await (async () => {
    let count = 0;
    await source().pipeTo(
      new WritableStream({
        write() {
          count++;
        },
      }),
    );
    return count;
  })(),
);

Sample output

2