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


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


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

用法:

writable.writableEnded 

返回值:如果可写,则返回true.end()方法被调用,否则返回false。


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

范例1:

// Node.js program to demonstrate the      
// writable.writableEnded 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 function 
writable.end(); 
  
// Calling writable.writableEnded 
// Property 
writable.writableEnded; 
  
// Calling destroy function 
//to display output 
writable.destroy();

输出:

hi
GFG
Writable {
  _writableState:
   WritableState {     objectMode:false,
     highWaterMark:16384,
     finalCalled:false,
     needDrain:false,
     ending:true,
     ended:true,
     finished:false,
     destroyed:true,
     decodeStrings:true,
     defaultEncoding:'utf8',
     length:0,
     writing:false,
     corked:0,
     sync:false,
     bufferProcessing:false,
     onwrite:[Function:bound onwrite],
     writecb:null,
     writelen:0,
     bufferedRequest:null,
     lastBufferedRequest:null,
     pendingcb:2,
     prefinished:true,
     errorEmitted:false,
     emitClose:true,
     autoDestroy:false,
     bufferedRequestCount:0,
     corkedRequestsFree:
      { next:null,
        entry:null,
        finish:[Function:bound onCorkedFinish] } },
  writable:false,
  _write:[Function:write],
  domain:null,
  _events:[Object:null prototype] {},
  _eventsCount:0,
  _maxListeners:undefined }

在这里,您可以看到上面示例中的end属性被设置为true。

范例2:

// Node.js program to demonstrate the      
// writable.writableEnded 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.writableEnded 
// Property 
writable.writableEnded; 
  
// Calling destroy function 
//to display output 
writable.destroy();

输出:

hi
GFG
Writable {
  _writableState:  WritableState {
     objectMode:false,
     highWaterMark:16384,
     finalCalled:false,
     needDrain:false,
     ending:false,
     ended:false,
     finished:false,
     destroyed:true,
     decodeStrings:true,
     defaultEncoding:'utf8',
     length:0,
     writing:false,
     corked:0,
     sync:false,
     bufferProcessing:false,
     onwrite:[Function:bound onwrite],
     writecb:null,
     writelen:0,
     bufferedRequest:null,
     lastBufferedRequest:null,
     pendingcb:2,
     prefinished:false,
     errorEmitted:false,
     emitClose:true,
     autoDestroy:false,
     bufferedRequestCount:0,
     corkedRequestsFree:
      { next:null,
        entry:null,
        finish:[Function:bound onCorkedFinish] } },
  writable:true,
  _write:[Function:write],
  domain:null,
  _events:[Object:null prototype] {},
  _eventsCount:0,
  _maxListeners:undefined }

在上面的示例中,将end属性设置为false,因为在调用writable.writableEnded属性之前未调用writable.end()方法。

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



相关用法


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