Node.js stream.finished() 方法
如果流不再可写或不可读,或者它遇到错误或不成熟的关闭事件,则使用stream.finished() 方法接收警报。
句法:
stream.finished(stream, options, callback)
参数:此方法接受三个参数,如上所述,如下所述:
- stream:这个参数可以是可读的,也可以是可写的。
- options:这是一个对象,可以是:
- 它可能是一个错误,即如果它设置为假,那么对发出事件('error',err)的调用不会被视为已完成,默认情况下它是真的。
- 它是可读的,即如果它设置为false,那么当流结束时调用一个回调函数,流仍然是可读的,默认情况下它是true。
- 它可以是可写的,即当设置为 false 时,当流结束时调用回调函数,但流仍然是可写的,默认情况下为 true。
- 回调:一个回调函数,它接受一个可选的错误参数。
返回值:它返回一个清除所有已注册侦听器的清理函数。
下面的例子说明了 Node.js 中stream.finished() 方法的使用:
示例 1:
javascript
// Node.js program to demonstrate the
// stream.finished(stream[, options],
// callback) method
// Including fs module
var fs = require('fs');
// Constructing finished from stream
const { finished } = require('stream');
// Constructing promisify from
// util
const { promisify } = require('util');
// Defining finishedAsync method
const finishedAsync = promisify(finished);
// Constructing readable stream
const readable = fs.createReadStream("input.text");
// Constructing writable stream
var writable = fs.createWriteStream("output.text");
// Async function
(async function run() {
try{
// Calling pipe method
readable.pipe(writable);
await finishedAsync(readable);
console.log("Readable is being consumed");
}
// Shows error
catch(err) {
console.error(err);
}
})();
javascript
// Node.js program to demonstrate the
// stream.finished(stream[, options],
// callback) method
// Including fs module
var fs = require('fs');
// Constructing finished from stream
const { finished } = require('stream');
// Constructing promisify from
// util
const { promisify } = require('util');
// Defining finishedAsync method
const finishedAsync = promisify(finished);
// Constructing readable stream
const readable = fs.createReadStream("input.text");
// Constructing writable stream
var writable = fs.createWriteStream("output.text");
// Async function
(async function run() {
try{
// Calling pipe method
readable.pipe(writable);
await finishedAsync(readable);
console.log("Readable is being consumed");
}
// Shows error
catch(err) {
console.error(err);
}
})();
输出:
Promise { }
Readable is being consumed
示例 2:
javascript
// Node.js program to demonstrate the
// stream.finished(stream[, options],
// callback) method
// Including fs module
var fs = require('fs');
// Constructing finished from stream
const { finished } = require('stream');
// Constructing promisify from
// util
const { promisify } = require('util');
// Defining finishedAsync method
const finishedAsync = promisify(finished);
// Constructing readable stream
const readable = fs.createReadStream("input.text");
// Constructing writable stream
var writable = fs.createWriteStream("output.text");
// Async function
(async function run() {
try{
// Calling pipe method
readable.pipe(writable);
await finishedAsync(readable);
console.log("Readable is being consumed");
}
// Shows error
catch(err) {
console.error(err);
}
})();
输出:这里,写入文件名时发生错误,因此在输出中返回错误。
Promise { }
{ [Error: ENOENT: no such file or directory, open 'input.text']
errno: -2, code: 'ENOENT', syscall: 'open', path: 'input.text' }
参考: https://nodejs.org/api/stream.html#stream_stream_finished_stream_options_callback