Node.js stream.finished() Method - GeeksforGeeks

11 Oct, 2021
The stream.finished() method is utilized to receive an alert if a stream is not writable or readable anymore or if it had experienced an error or a close event that is immature. 
Syntax:  
stream.finished(stream, options, callback)
Parameters: This method accept three parameters as mentioned above and described below:  
stream: This parameter can be readable or writable.
options: This is an object, which can be: It can be an error i.e. if it is set to false, then a call to emit events(‘error’, err) is not considered as finished and by default it is true.It can be readable i.e. if it is set to false, then a callback function is called when the stream ends still the stream can be readable and by default it is true.It can be writable i.e. When set to false, then a callback function is called when the stream ends still the stream can be writable and by default it is true.
It can be an error i.e. if it is set to false, then a call to emit events(‘error’, err) is not considered as finished and by default it is true.It can be readable i.e. if it is set to false, then a callback function is called when the stream ends still the stream can be readable and by default it is true.It can be writable i.e. When set to false, then a callback function is called when the stream ends still the stream can be writable and by default it is true.
It can be an error i.e. if it is set to false, then a call to emit events(‘error’, err) is not considered as finished and by default it is true.
It can be readable i.e. if it is set to false, then a callback function is called when the stream ends still the stream can be readable and by default it is true.
It can be writable i.e. When set to false, then a callback function is called when the stream ends still the stream can be writable and by default it is true.
callback: A callback function which takes an elective error argument.
Return Value: It returns a cleanup function which detaches all the registered listeners.Below examples illustrate the use of stream.finished() method in Node.js:Example 1:  
javascript
// Node.js program to demonstrate the     // stream.finished(stream[, options], // callback) method  // Including fs modulevar fs = require('fs');  // Constructing finished from streamconst { finished } = require('stream');  // Constructing promisify from// utilconst { promisify } = require('util');  // Defining finishedAsync methodconst finishedAsync = promisify(finished);  // Constructing readable streamconst readable = fs.createReadStream("input.text");  // Constructing writable streamvar writable = fs.createWriteStream("output.text");  // Async function(async function run() {  try{       // Calling pipe method    readable.pipe(writable);    await finishedAsync(readable);    console.log("Readable is being consumed");    }    // Shows error  catch(err) {    console.error(err);  }  })();
Output: 
Promise { <pending>  }
Readable is being consumed
Example 2:  
javascript
// Node.js program to demonstrate the     // stream.finished(stream[, options], // callback) method  // Including fs modulevar fs = require('fs');  // Constructing finished from streamconst { finished } = require('stream');  // Constructing promisify from// utilconst { promisify } = require('util');  // Defining finishedAsync methodconst finishedAsync = promisify(finished);  // Constructing readable streamconst readable = fs.createReadStream("input.text");  // Constructing writable streamvar writable = fs.createWriteStream("output.text");  // Async function(async function run() {  try{       // Calling pipe method    readable.pipe(writable);    await finishedAsync(readable);    console.log("Readable is being consumed");    }    // Shows error  catch(err) {    console.error(err);  }  })();
Output: Here, an error occurs while writing the file name so an error is returned in the output. 
Promise { <pending>  }
{ [Error: ENOENT: no such file or directory, open 'input.text'] 
errno: -2, code: 'ENOENT', syscall: 'open', path: 'input.text' }
Reference: https://nodejs.org/api/stream.html#stream_stream_finished_stream_options_callback
Akanksha_Rai
sweetyty
Node.js-Stream-module
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Node.js fs.readFile() Method
Node.js fs.writeFile() Method
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
How to read and write Excel file in Node.js ?
Roadmap to Become a Web Developer in 2022
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
Convert a string to an integer in JavaScript