Open In App

Node.js Stream writable.end() Method

Last Updated : 12 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The writable.end() method is an inbuilt application programming interface of Stream module so that no more data can be written to the Writable anymore. The arguments chunk and encoding are optional which will permit one final new chunk of data to be written instantly before closing the stream. Moreover, the optional callback function is added as a listener for the ‘finish’ event of the Writable stream.

Syntax:

writable.end( chunk, encoding, callback)

Parameters: This method accepts three parameters as mentioned above and described below:

  • chunk: It is an optional data to write. The value of chunk must be a string, buffer or Uint8Array. For object mode, the chunk value may be anything other than null.
  • encoding: It holds the encoding value if chunk is a string value.
  • callback: It is an optional callback function for stream.

Return Value: It returns the data written before calling this method and if the end() method has a chunk of new data then that is also returned at the end.

Below examples illustrate the use of writable.end() method in Node.js:

Example 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!");
});


Output:

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!

Example 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');


Output:

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!

Here an error is displayed as write() method is called after end() method which is not possible.

Reference: https://nodejs.org/api/stream.html#stream_writable_end_chunk_encoding_callback



Previous Article
Next Article

Similar Reads

Node.js Stream writable.writable Property
The writable.writable property is an inbuilt application programming interface of Stream module which is used to check the writable.write() method is safe to call or not. Syntax: writable.writable Return Value: It returns true if it is safe to call writable.write() method otherwise returns false. Below examples illustrate the use of writable.writab
2 min read
Node.js Stream writable.cork() Method
The writable.cork() method is an inbuilt application programming interface of Stream module which is used to write every data into the buffer memory. When we use stream.uncork() or stream.end() methods then the buffer data will be flushed. Syntax: writable.cork() Parameters: This method does not accept any parameters. Return Value: If this method i
2 min read
Node.js Stream writable.write() Method
The writable.write() method is an inbuilt application programming interface of Stream module which is used to write some data to the Writable stream. The callback function is called once the data has been completely handled. Syntax: writable.write( chunk, encoding, callback) Parameters: This method accepts three parameters as mentioned above and de
2 min read
Node.js Stream writable.destroy() Method
The writable.destroy() method is an inbuilt application programming interface of Stream module which is used to destroy the created stream and you cannot call the write() method to write data again after you have already destroyed the created stream. Syntax: writable.destroy() Parameters: This method does not accept any parameters. Return Value: It
2 min read
Node.js Stream writable.uncork() Method
The writable.uncork() method is an inbuilt application programming interface of Stream module which is used to flush all the buffered data when stream.cork() method was called. Syntax: writable.uncork() Parameters: This method does not accept any parameters. Return Value: If this method is being called then the data which was being corked is again
2 min read
Node.js Stream writable.setDefaultEncoding() Method
The writable.setDefaultEncoding() method is an inbuilt application programming interface of Stream module which is used to set the default encoding for a Writable stream. Syntax: writable.setDefaultEncoding( encoding ) Parameters: This method accepts single parameter encoding which holds the encoding to be used for the Writable stream. Return Value
2 min read
Node.js Stream writable._write() Method
The writable._write() method is an inbuilt application programming interface of Stream module which is used to implement a writable stream. The writable._write() method is affixed with an underscore as it is inside the class that defines it. Moreover, the user program must not call it directly. This method implemented by using child classes and it
3 min read
Node.js Stream writable.writableFinished Property
The writable.writableFinished property is set to true instantly before the emit of the 'finish' event. Syntax: writable.writableFinished Return Value: It returns true if 'finish' event is called before it else it returns false. Below examples illustrate the use of writable.writableFinished property in Node.js: Example 1: // Node.js program to demon
2 min read
Node.js Stream writable.writableObjectMode Property
The writable.writableObjectMode property in Stream module is used to get the object mode value of the Writable stream. Syntax: writable.writableObjectMode Return Value: It returns true if the objectMode is set to true otherwise returns false. Below examples illustrate the use of writable.writableObjectMode property in Node.js: Example 1: // Node.js
2 min read
Node.js Stream writable.writableLength Property
The writable.writableLength property is an inbuilt application of stream module which is used to check the number of bytes in the queue that is ready to be written. Syntax: writable.writableLength Return Value: This property returns the number of bytes that is ready to write into the queue. Below examples illustrate the use of writable.writableLeng
2 min read