📜  Node.js Stream writable.writableFinished 属性

📅  最后修改于: 2022-05-13 01:56:32.159000             🧑  作者: Mango

Node.js Stream writable.writableFinished 属性

writable.writableFinished 属性在 'finish' 事件发出之前立即设置为 true。

句法:

writable.writableFinished

返回值:如果'finish'事件在它之前被调用,则返回true,否则返回false。
下面的例子说明了 Node.js 中writable.writableFinished 属性的使用:

示例 1:

// Node.js program to demonstrate the     
// writable.writableFinished Property
   
// Accessing stream module
const stream = require('stream');
   
// Creating a stream and creating 
// a write function
const writable = new stream.Writable({
   
  // Write function with its 
  // parameters
  write: function(chunk, encoding, next) {
   
    // Converting the chunk of
    // data to string
    console.log(chunk.toString());
    next();
  }
});
    
// Using for loop and calling 
// write method 
for (let i = 0; i < 5; i++) {
  writable.write(`GfG, #${i}!`);
}
   
// Emitting finish event
writable.on('finish', () => {
  console.log('All writes are now complete.');
});
   
// Calling end function
writable.end('This is the end\n');
   
// Calling writable.writableFinished  
// Property
writable.writableFinished;
writable.destroy();

输出:

GfG, #0!
GfG, #1!
GfG, #2!
GfG, #3!
GfG, #4!
This is the end

All writes are now complete.

示例 2:

// Node.js program to demonstrate the     
// writable.writableFinished Property
   
// Accessing stream module
const stream = require('stream');
   
// Creating a stream and creating 
// a write function
const writable = new stream.Writable({
   
  // Write function with its 
  // parameters
  write: function(chunk, encoding, next) {
   
    // Converting the chunk of
    // data to string
    console.log(chunk.toString());
    next();
  }
});
   
// Calling write() method
writable.write('GfG');
   
// Calling writable.writableFinished  
// Property
writable.writableFinished;
writable.destroy();

输出

GfG

在上面的示例中,输出为 false,因为在writable.writableFinished属性之前未调用 'finish' 事件。

参考: https://nodejs.org/api/stream.html#stream_writable_writablefinished。