La propiedad writable.writableEnded es una interfaz de programación de aplicaciones incorporada del módulo Stream que se utiliza para comprobar si se llama o no al método writable.end().
Sintaxis:
writable.writableEnded
Valor devuelto: Devuelve verdadero si se llama al método writable.end() antes, de lo contrario, devuelve falso.
Los siguientes ejemplos ilustran el uso de la propiedad writable.writableEnded en Node.js:
Ejemplo 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();
Producción:
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 }
Aquí, puede ver que la propiedad finalizada en el ejemplo anterior se establece en verdadero.
Ejemplo 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();
Producción:
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 }
En el ejemplo anterior, la propiedad terminó se establece en falso porque el método writable.end() no se llama antes de llamar a la propiedad writable.writableEnded.
Referencia: https://nodejs.org/api/stream.html#stream_writable_writableended
Publicación traducida automáticamente
Artículo escrito por nidhi1352singh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA