Node.js Stream writable.writableEnded 属性
writable.writableEnded 属性是 Stream 模块的内置应用程序编程接口,用于检查 writable.end() 方法是否被调用。
句法:
writable.writableEnded
返回值:如果在调用 writable.end() 方法之前返回 true,否则返回 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