Dev Helpers

Node.js / Web streams

Collect a web stream with for-await

Dev Helpers · April 25, 2026

An async loop consumes a web stream chunk by chunk. Stream chunks need not align with logical messages or character boundaries.

Worked example

const source = () =>
  new ReadableStream({
    start(c) {
      c.enqueue("a");
      c.enqueue("b");
      c.close();
    },
  });
console.log(await Array.fromAsync(source()));

Result

[ 'a', 'b' ]

Implementation notes

A text decoder needs to handle characters split across byte chunks. Decode as a stream if the source contains arbitrary UTF-8 text.

Try it yourself

Split one logical record across two chunks and reassemble it.

Open the Node.js example →

Editorial date April 25, 2026 · Published December 6, 2026

Related articles

Safely parse malformed input

Parsing user-provided JSON needs a failure path. Successful parsing alone does not establish that the result has the expected shape.

Checksum bytes with CRC32

CRC32 is useful for detecting accidental changes to a byte sequence. Record the input encoding so different producers compute the same checksum.

Attach details to a mark

Performance marks can carry details about the operation they describe. Small stable identifiers are easier to aggregate than large payloads.