Just try
Alican Çubukçuoğlu
alicancubukcuoglu at gmail.com
Fri Oct 30 18:17:02 UTC 2015
The recommended way of checking for file existence in Node.js is calling
`fs.stat()` on the file. If no error was passed to the callback, it exists.
If "ENOENT" was passed, it doesn't.
If you "promisify" `fs.stat()` and use it with async/await, it throws when
a file doesn't exist so you end up writing a lot of try/catches. You say
"Hey, it would be great if I didn't have to keep writing `catch(e){}`." but
what if the error wasn't "ENOENT"?
That's why I withdrew myself from suggesting such a thing. Carelessly
silencing errors is no good.
I like the idea of `let stuff = try something()` putting the error in
`stuff` but the problem is you can throw strings in JavaScript:
```
function getUserName() {
throw 'Error';
}
const userName = try getUserName();
if (userName instanceof Error) {
handleError(userName);
return;
}
console.log('There was no error, yay!');
// Actually there was
```
---
Also `try stuff() || undefined` will not be evaluated to `undefined` on
error:
```
> var err = new Error('Stuff blew up!');
undefined
> err || undefined
[Error: Stuff blew up!]
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151030/c63dd413/attachment.html>
More information about the es-discuss
mailing list