當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。