Node.js Stream writable.destroy() Method - GeeksforGeeks

12 Oct, 2021
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 returns all the properties of the Writable in which destroyed property is set to true.
Below examples illustrate the use of writable.cork() method in Node.js:
Example 1:
// Node.js program to demonstrate the     // writable.uncork() method  const stream = require('stream');  // Creating a stream and creating // a write functionconst 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 datawritable.write('hi');  // Again writing some datawritable.write('hello');  // Calling destroy functionwritable.destroy();
Output:
hi
hello
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 }

So, the created stream is destroyed.
Example 2:
// Node.js program to demonstrate the     // writable.uncork() method  const stream = require('stream');  // Creating a stream and creating // a write functionconst 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 datawritable.write('hi');  // Again writing some datawritable.write('hello');  // Calling destroy functionwritable.destroy();  writable.write('');
Output:
hi
hello
Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destro
yed
    at doWrite (_stream_writable.js:411:19)
    at writeOrBuffer (_stream_writable.js:399:5)
    at Writable.write (_stream_writable.js:299:11)
    at /home/runner/QuizzicalFluffyOperation/index.js:29: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)

In the above example, an error occurs because the write() method was called after the stream was destroyed.
Reference: https://nodejs.org/api/stream.html#stream_writable_destroy_error
Node.js-Stream-module
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
Node.js fs.readFileSync() Method
Node.js fs.writeFile() Method
How to update NPM ?
Difference between promise and async await in Node.js
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript