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


node.js Stream readable.readableEnded用法及代碼示例


可讀流中的visible.visibleEnded屬性用於檢查是否發出了結束事件。

用法:

readable.readableEnded

返回值:如果發出結束事件,則返回true,否則返回false。


下麵的示例說明了Node.js中visible.visibleEnded屬性的使用:

範例1:

// Node.js program to demonstrate the      
// readable.readableEnded Property   
  
// Include fs module 
var fs = require("fs"); 
  
// Data to be displayed 
var data = ''; 
  
// Create a readable stream 
var readable = fs.createReadStream("input.text"); 
  
// Set the encoding to be utf8.  
readable.setEncoding('UTF8'); 
  
// Handling stream event data and end 
readable.on('data', function(chunk) { 
   data += chunk; 
}); 
  
// End event 
readable.on('end',function() { 
   console.log(data); 
}); 
  
// Calling readableEnded property 
readable.readableEnded;

輸出:

read:hello
true
end!!

範例2:

// Node.js program to demonstrate the      
// readable.readableEnded Property   
  
// Include fs module 
var fs = require("fs"); 
  
// Create a readable stream 
var readable = fs.createReadStream("input.text"); 
  
// Set the encoding to be utf8.  
readable.setEncoding('UTF8'); 
readable.on('readable', () => { 
  let chunk; 
  
  // Using while loop and calling 
  // read method with parameter 
  while (null !== (chunk = readable.read())) { 
  
    // Displaying the chunk 
    console.log(`read:${chunk}`); 
  } 
}); 
  
// Calling readableEnded property 
readable.readableEnded;

輸出:

false
read:hello

參考: https://nodejs.org/api/stream.html#stream_readable_readableended



相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | Stream readable.readableEnded Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。