I'm not able to catch ENOENT of fs.createReadStream(). Is this an asynchronous function , which throws exception in a different closure-chain ?
$ node -v v0.10.9 $ cat a.js fs = require('fs') try { x = fs.createReadStream('foo'); } catch (e) { console.log("Caught" ); } $ node a.js events.js:72 throw er; // Unhandled 'error' event ^ Error: ENOENT, open 'foo'
I am expecting 'Caught' to be printed rather than error stack !
The function fs. createReadStream() allows you to open up a readable stream in a very simple manner. All you have to do is pass the path of the file to start streaming in. It turns out that the response (as well as the request) objects are streams.
createReadStream() appears to have a synchronous interface. It does not return a promise or accept a callback to communicate back when it's done or to send back some results. So, it appears synchronous from the interface.
fs.createReadStream
is asynchronous with the event emitter style and does not throw exceptions (which only make sense for synchronous code). Instead it will emit an error event.
const fs = require('fs') const stream = fs.createReadStream('foo'); stream.on('error', function (error) {console.log("Caught", error);}); stream.on('ready', function () {stream.read();});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With