The ‘close’ Event in a Readable Stream is emitted when the stream and any of its hidden resources are being closed This event implies that no further events can be emitted, and no further computations can take place. Moreover, if a Readable stream is created with the emitClose option then it can always emit ‘close’ event.
Syntax:
Event: 'close '
Below examples illustrate the use of close event in Node.js:
Example 1:
// Node.js program to demonstrate the // readable close event // Including fs module const fs = require('fs'); // Constructing readable stream const readable = fs.createReadStream("input.txt"); // Calling close method readable.close(); // Handling close event readable.on("close", () => { console.log("Stream ended"); }); console.log("Done..."); |
Output:
Done... Stream ended
Example 2:
// Node.js program to demonstrate the // readable close event // Including fs module const fs = require('fs'); // Constructing readable stream const readable = fs.createReadStream("input.txt"); // Handling close event readable.on("close", () => { console.log("Stream ended"); }); console.log("Done..."); |
Output:
Done...
Here, close method is not called so close event is not emitted.
Reference: https://nodejs.org/api/stream.html#stream_event_close_1.
Recommended Posts:
- Node.js | Readable Stream readable Event
- Node.js | Stream readable.readable Property
- Node.js | Readable Stream error Event
- Node.js | Readable Stream end Event
- Node.js | Readable Stream data Event
- Node.js | Readable Stream resume Event
- Node.js | Readable Stream pause Event
- Node.js | stream.Writable close Event
- Node.js | Stream readable.pause() Method
- Node.js | Stream readable.isPaused() Method
- Node.js | Stream readable.destroyed Property
- Node.js | Stream readable.destroy() Method
- Node.js | Stream readable.read() Method
- Node.js | Stream readable.readableEncoding Property
- Node.js | Stream readable.readableEnded Property
- Node.js | Stream readable.readableFlowing Property
- Node.js | Stream readable.readableHighWaterMark Property
- Node.js | Stream readable.readableLength Property
- Node.js | Stream readable.readableObjectMode Property
- Node.js | Stream readable.pipe() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

