Async stream pipelines
Connect readable, transform, and writable streams while awaiting completion and catching failures.
Node.js · Module
node:stream/promises provides promise-returning utilities for working with streams. Use it with async/await to compose pipelines and await stream completion instead of supplying callbacks.
The module exposes promise-based stream utilities, including pipeline and finished. Import them from node:stream/promises and await their returned promises; pipeline connects streams, propagates errors, and resolves after completion, while finished resolves when a stream ends or closes according to its options.
import { pipeline, finished } from 'node:stream/promises';Exports promise-based stream utility functions, including pipeline and finished; pipeline returns a Promise that fulfills when the pipeline is complete.
import { pipeline } from 'node:stream/promises';
import { createReadStream, createWriteStream } from 'node:fs';
import { createGzip } from 'node:zlib';
try {
await pipeline(
createReadStream('input.txt'),
createGzip(),
createWriteStream('input.txt.gz')
);
console.log('Pipeline complete');
} catch (error) {
console.error('Pipeline failed', error);
}Compresses a file with a promise-returning stream pipeline; the process waits for completion and handles rejection.
Connect readable, transform, and writable streams while awaiting completion and catching failures.
Wait for a stream lifecycle event before releasing resources or continuing dependent work.
Process compatible Web Streams with promise-based stream utilities on supported Node.js versions.
Import the utilities from node:stream/promises. Unlike callback-oriented stream helpers, these functions return promises, so async/await and try/catch can handle completion and failures.
Follow-up: What happens when a pipeline stage fails? The returned promise rejects, allowing surrounding error handling to catch the failure while the pipeline performs its stream cleanup behavior.
pipeline accepts a source, optional transforms, and a destination, then returns a promise that fulfills when the pipeline completes. It is useful when completion must be coordinated with subsequent asynchronous work.
Follow-up: How would you keep the destination open? Supply the options object with end: false, which disables automatic destination closure when the source ends.
The module is available through require('node:stream/promises') and also through require('node:stream').promises. The dedicated subpath makes the promise-based API explicit.
Follow-up: Can pipeline consume web streams? Yes, the documentation records web-stream support as added in v18.16.0 and v19.7.0, so runtime compatibility matters.
stream.pipelinestream.finishednode:streamnode:fsnode:zlib