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
相關用法
- node.js Stream writable.writableLength用法及代碼示例
- node.js Stream writable.destroyed用法及代碼示例
- node.js Stream readable.destroyed用法及代碼示例
- node.js Stream writable.writableCorked用法及代碼示例
- node.js Stream writable.writableHighWaterMark用法及代碼示例
- node.js Stream writable.writableFinished用法及代碼示例
- node.js Stream writable.writableObjectMode用法及代碼示例
- node.js Stream readable.readable用法及代碼示例
- node.js Stream writable.writable用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | Stream writable.writableEnded Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。