stream.finished(stream[, options])
-
stream
<Stream> | <ReadableStream> | <WritableStream> 可读和/或可写流/网络流。¥
stream
<Stream> | <ReadableStream> | <WritableStream> A readable and/or writable stream/webstream. -
options
<Object>-
error
<boolean> | <undefined> -
readable
<boolean> | <undefined> -
writable
<boolean> | <undefined> -
signal
<AbortSignal> | <undefined> -
cleanup
<boolean> | <undefined> 如果是true
,则在履行 promise 之前删除此函数注册的监听器。默认值:false
。¥
cleanup
<boolean> | <undefined> Iftrue
, removes the listeners registered by this function before the promise is fulfilled. Default:false
.
-
-
返回:<Promise> 当流不再可读或可写时执行。
¥Returns: <Promise> Fulfills when the stream is no longer readable or writable.
const { finished } = require('node:stream/promises');
const fs = require('node:fs');
const rs = fs.createReadStream('archive.tar');
async function run() {
await finished(rs);
console.log('Stream is done reading.');
}
run().catch(console.error);
rs.resume(); // Drain the stream.
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.
finished
API 还提供了 回调版本。
¥The finished
API also provides a callback version.
stream.finished()
在返回的 promise 得到解决或拒绝后,留下悬而未决的事件监听器(特别是 'error'
、'end'
、'finish'
和 'close'
)。这样做的原因是意外的 'error'
事件(由于不正确的流实现)不会导致意外的崩溃。如果这是不想要的行为,则应将 options.cleanup
设置为 true
:
¥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 });