📜  Node.js Stream writable.destroyed 属性

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

Node.js Stream writable.destroyed 属性

writable.destroyed 属性是 Stream 模块的内置应用程序编程接口,用于检查 writable.destroy() 方法是否被调用。

句法:

writable.destroyed

返回值:如果 writable.destroy() 方法在它之前被调用,则此属性返回 true,否则返回 false。

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

示例 1:

// Node.js program to demonstrate the     
// writable.distroyed 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('hello');
  
// Calling destroy function
writable.destroy();
  
// Calling the Property
writable.destroyed;

输出:

hi
hello
true

示例 2:

// Node.js program to demonstrate the     
// writable.distroyed 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('hello');
  
// Calling the Property
writable.destroyed;

输出:

hi
hello
false

在上面的示例中,输出为 false,因为在调用属性 writable.destroyed 之前未调用writable.destroy()方法。

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