📜  Node.js Stream writable.writableCorked 属性

📅  最后修改于: 2022-05-13 01:56:52.311000             🧑  作者: Mango

Node.js Stream writable.writableCorked 属性

writable.writableCorked 属性是 Stream 模块的内置应用程序编程接口,用于检查您需要调用 uncork()函数的次数,以便您可以完全解开流。

句法:

writable.writableCorked 

返回值:它返回一个整数值,表示需要调用 writable.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