function
stream.promises.finished
): Promise<void>;
import { finished } from 'node:stream/promises';
import { createReadStream } from 'node:fs';
const rs = createReadStream('archive.tar');
async function run() {
await finished(rs);
console.log('Stream is done reading.');
}
run().catch(console.error);
rs.resume(); // Drain the stream.The finished API also provides a callback version.
stream.finished() leaves dangling event listeners (in particular 'error', 'end', 'finish' and 'close') after the returned promise is resolved or rejected. The reason for this is so that unexpected 'error' events (due to incorrect stream implementations) do not cause unexpected crashes. If this is unwanted behavior then options.cleanup should be set to true:
await finished(rs, { cleanup: true });@returns
Fulfills when the stream is no longer readable or writable.
Referenced types
interface ReadableStream<R = any>
- ): ReadableStream<T>;
interface WritableStream<W = any>
interface FinishedOptions
- cleanup?: boolean
If true, removes the listeners registered by this function before the promise is fulfilled.