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


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


writable.writable属性是Stream模块的内置应用程序编程接口,用于检查writable.write()方法是否可以安全调用。

用法:

writable.writable 

返回值:如果可以安全地调用writable,则返回true。write()方法否则返回false。


以下示例说明了Node.js中writable.writable属性的使用:

范例1:

// Node.js program to demonstrate the      
// writable.writable 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(); 
  } 
}); 
  
// Writing data 
writable.write('hi'); 
  
// Again writing some data 
writable.write('GFG'); 
  
// Calling writable.writable 
// Property 
writable.writable;

输出:

hi
GFG
true

范例2:

// Node.js program to demonstrate the      
// writable.writable 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(); 
  } 
}); 
  
// Writing data 
writable.write('hi'); 
  
// Again writing some data 
writable.write('GFG'); 
  
// Calling end() method 
writable.end(); 
  
// Calling writable.writable 
// Property 
writable.writable;

输出:

hi
GFG
false

在上面的示例中,输出为false,因为在调用writable.writable属性之前调用了writable.end()方法,因此调用writable.write()方法并不安全,因此输出为false。

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



相关用法


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