当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


node.js Stream writable.writableFinished用法及代码示例


在发出“结束”事件之前,writable.writableFinished属性立即设置为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



相关用法


注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Node.js | Stream writable.writableFinished Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。