Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

ReadableStream.pipeTo — piping to a writable stream

pipeTo() transfers chunks to a WritableStream, respects backpressure, and returns a promise for completion.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

readable.pipeTo(destination, { preventClose: false, preventAbort: false, preventCancel: false, signal })

ReadableStream.pipeTo transfers chunks to a WritableStream. It returns a Promise that fulfills with undefined on success. By contrast, pipeThrough() returns the supplied pair’s existing readable stream without creating a new one.

Piping respects backpressure: reading follows the destination's readiness. It locks both streams and releases the locks when finished; an existing lock on either stream causes promise rejection with TypeError.

By default, source closure closes the destination, a source error aborts the destination, and a destination error cancels the source. Respectively, preventClose, preventAbort, and preventCancel disable that propagation; all default to false. They do not suppress promise rejection. signal accepts an AbortSignal; aborting cancels the source and aborts the destination, subject to those options.

Handle rejection using await with try/catch or .catch(). This is a WHATWG Streams Web API, not a feature of a particular ECMAScript edition. Check availability in the target browser; Node 24 does not establish browser support.

Examples

(async () => {
  const chunks = [];
  const readable = new ReadableStream({
    start(controller) {
      controller.enqueue("A");
      controller.enqueue("B");
      controller.close();
    }
  });
  const writable = new WritableStream({
    write(chunk) {
      chunks.push(chunk);
    },
    close() {
      chunks.push("closed");
    }
  });

  await readable.pipeTo(writable);
  console.log(chunks.join("|"));
  console.log(readable.locked, writable.locked);
})().catch(console.error);

Result

A|B|closed
false false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources