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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。