當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


node.js Stream writable.destroy()用法及代碼示例


writable.destroy()方法是Stream模塊的內置應用程序編程接口,用於銷毀所創建的流,並且在銷毀所創建的流之後,您無法調用write()方法來再次寫入數據。

用法:

writable.destroy()

參數:此方法不接受任何參數。


返回值:它返回其中Writable屬性設置為true的Writable的所有屬性。

以下示例說明了Node.js中writable.cork()方法的使用:

範例1:

// Node.js program to demonstrate the      
// writable.uncork() method   
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();

輸出:

hi
hello
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 }

因此,創建的流被銷毀。

範例2:

// Node.js program to demonstrate the      
// writable.uncork() method   
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(); 
  
writable.write('');

輸出:

hi
hello
Error [ERR_STREAM_DESTROYED]:Cannot call write after a stream was destro
yed
    at doWrite (_stream_writable.js:411:19)
    at writeOrBuffer (_stream_writable.js:399:5)
    at Writable.write (_stream_writable.js:299:11)
    at /home/runner/QuizzicalFluffyOperation/index.js:29:10
    at Script.runInContext (vm.js:133:20)
    at Object. (/run_dir/interp.js:156:20)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:
10)
    at Module.load (internal/modules/cjs/loader.js:653:32)

在上麵的示例中,發生錯誤是因為在銷毀流之後調用了write()方法。

參考: https://nodejs.org/api/stream.html#stream_writable_destroy_error



相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | Stream writable.destroy() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。