WritableStream Sink Receives Chunks

Build a WritableStream with write, close, and abort handlers, pipe a ReadableStream into it, and collect the transformed chunks.

JavascriptData Fetching

Code

javascriptsnippet
30 lines
const received = [];

const sink = new WritableStream({
  write(chunk) {
    received.push(String(chunk).toUpperCase());
  },
  close() {
    console.log("sink closed");
  },
  abort(reason) {
    console.log("sink aborted:", reason);
  },
});

const source = new ReadableStream({
  start(controller) {
    controller.enqueue("alpha");
    controller.enqueue("beta");
    controller.enqueue("gamma");
    controller.close();
  },
});

async function main() {
  await source.pipeTo(sink);
  console.log("received:", received.join("|"));
  console.log("count:", received.length);
}

main();

Output

Captured from a sandboxed run of this snippet in the build pipeline.

sink closed
received: ALPHA|BETA|GAMMA
count: 3

How it works

Count the function main as the entire surface area of WritableStream Sink Receives Chunks.

Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed.

Applying it

The primary signature is async function main()

Output lines are prefixed (sink closed, received:, count:), making each step’s effect easy to spot.

Statements that matter

await source.pipeTo(sink);

FAQ

What does WritableStream Sink Receives Chunks print when run?
No setup needed: run the file and look for labeled lines like `sink closed`, `received:`, `count:` in the output.