-
Notifications
You must be signed in to change notification settings - Fork 341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add throws
option for readFile (async)
#39
Add throws
option for readFile (async)
#39
Conversation
Thanks for your interest.
|
The bigger thing (and this is still not a huge thing, but it's definitely a nicety) is related to use with promises, especially when used with compiled es6 code using default parameters (we use a lot of babel). in a promise chain, assuming this has been promisified, we get (in its most condensed format) .then(() => {
return jsonFile.read('someFilePath')
.catch(() => { return {}; });
})
.then((jsonOutput) => {
//do some stuff
}) There are a few ways to write that clearly, but that's the most condensed way. With the additon of the option for .then(() => {
return jsonFile.read('someFilePath', {throws: false})
})
.then((jsonOutput = {}) => {
//do some stuff
}) which while not light years apart, avoids the weird, potentially unclear-in-intention catch statement. I realize fully that it's not a huge loss having the weird catch, but calling JSON.parse on the output of an fs.readFile isn't really either. More of just a convenience added, which is really the whole point of the module, no? |
Ok, I think you make a good case on both points. I definitely start concepts with sync and then move to async. I'm starting to think that maybe |
Sorry I'm just coming back to this. I'll have a little time later to finally add a test as well 👍 |
By later I totally meant like half an hour later, apparently. |
Excellent, thank you! Why the name |
I can fix that, I just didn't remember what the term was off the top of my head. Can fix 👍 |
Although on second hand, I don't know that 'errorOnParse' is exactly clear, since it doesn't error when you parse, it passes along the error when the parse fails. Hrm. I mean, I'll yield to your judgement on that, but errorOnParse doesn't seem super clear (although errorOnFailedParse seems way too verbose). |
Haha, I feel the same way. I'm open to another name? On Thursday, December 17, 2015, Max Korp [email protected] wrote:
Simple & Secure Bitcoin Wallet: https://www.coinbolt.com |
if it wasn't an inversion to the api i'd suggest something like What about something like |
Ran with that, as it was the best I could come up with. |
Sorry for the incredibly long delay, thanks for your work on this. |
Published. However, please don't use |
This maintains the behavior of readFileSync.
Just like an error actually reading the file would still throw, in this case it will still return the error to the callback.
If an error happens parsing the contents, the callback will no longer recieve an error, but
null
will be passed back to the callback for the object, just as the sync version returnsnull
if it fails to parse. If the option is omitted (or still true) existing behavior will be maintained.