writable.end()方法是Stream模塊的內置應用程序編程接口,因此不再可以將更多數據寫入Writable。參數塊和編碼是可選的,這將允許在關閉流之前立即寫入一個最終的新數據塊。此外,可選的回調函數被添加為可寫流的“完成”事件的偵聽器。
用法:
writable.end( chunk, encoding, callback)
參數:此方法接受上述和以下所述的三個參數:
- chunk:它是要寫入的可選數據。塊的值必須是字符串,緩衝區或Uint8Array。對於對象模式,塊值可以是null以外的任何值。
- encoding:如果塊是字符串值,它將保存編碼值。
- callback:它是流的可選回調函數。
返回值:它返回在調用此方法之前寫入的數據,如果end()方法具有大量新數據,則該數據也將在最後返回。
以下示例說明了Node.js中writable.end()方法的使用:
範例1:
// Node.js program to demonstrate the
// writable.end() method
// INcluding 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');
// Calling end method with its
// all the parameters
writable.end("last data", "utf8", () => {
console.log("Writable stream ended!");
});
輸出:
hi last data Writable { _writableState: WritableState { objectMode:false, highWaterMark:16384, finalCalled:false, needDrain:false, ending:true, ended:true, finished:false, destroyed:false, 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] { finish:{ [Function:bound onceWrapper] listener:[Function] } }, _eventsCount:1, _maxListeners:undefined } Writable stream ended!
範例2:
// Node.js program to demonstrate the
// writable.end() method
// INcluding 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');
// Calling end method with its
// all the parameters
writable.end("last data", "utf8", () => {
console.log("Writable stream ended!");
});
writable.write('GfG');
輸出:
hi last data Error [ERR_STREAM_WRITE_AFTER_END]:write after end at writeAfterEnd (_stream_writable.js:248:12) at Writable.write (_stream_writable.js:296:5) at /home/runner/LuxuriousLegitimateObservation/index.js:30: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) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3)Writab le stream ended!
此處顯示錯誤,因為在end()方法之後調用了write()方法,這是不可能的。
參考: https://nodejs.org/api/stream.html#stream_writable_end_chunk_encoding_callback
相關用法
- node.js Stream readable.isPaused()用法及代碼示例
- node.js Stream readable.pause()用法及代碼示例
- node.js Stream writable.write()用法及代碼示例
- node.js Stream writable.cork()用法及代碼示例
- node.js Stream writable.uncork()用法及代碼示例
- node.js Stream writable.destroy()用法及代碼示例
- node.js Stream readable.read()用法及代碼示例
- node.js Stream writable.setDefaultEncoding()用法及代碼示例
- node.js Stream readable.destroy()用法及代碼示例
- Node.js GM blur()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | Stream writable.end() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。