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


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


writable.writableCorked属性是Stream模块的内置应用程序编程接口,该接口用于检查调用uncork()函数所需的次数,以便完全取消插播流。

用法:

writable.writableCorked 

返回值:它返回一个表示可写次数的整数值。需要调用uncork()。


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

范例1:

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

输出:

2

范例2:

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

输出:

hi
1

在上面的示例中,我们已经调用了uncork()函数一次。因此,为了完全解开流,您只需要调用uncork()函数一次,输出为1。

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



相关用法


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