What's the difference between piping streams with pipeThrough() and pipeTo()?
Short answer
pipeTo() sends a ReadableStream's data into a WritableStream and returns a Promise that resolves when piping finishes. pipeThrough() sends data through a TransformStream (readable + writable in one) and returns a new ReadableStream you can keep chaining or reading further.
Advertisement
AModel answer
pipeTo() sends a ReadableStream's data into a WritableStream and returns a Promise that resolves when piping finishes. pipeThrough() sends data through a TransformStream (readable + writable in one) and returns a new ReadableStream you can keep chaining or reading further.
const transformed = response.body
.pipeThrough(new TextDecoderStream())
.pipeThrough(new SomeTransform());
await transformed.pipeTo(writableStream);Real-world example
Decoding, transforming, and compressing a file stream in one pipeline without buffering it all in memory.
Likely follow-ups
- How do you handle errors that occur mid-pipeline?
Related topics