On Fri, Oct 30, 2015 at 2:17 PM, Alican Çubukçuoğlu <
alicancubukcuo...@gmail.com> wrote:

> 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
>


I think you missed the tuple destructuring in the example try-expression.
It wasn't:

```js
let errOrValue = try JSON.parse('invalid');
```

But instead:

```js
let [err, result] = try JSON.parse('invalid');
```

This avoids the problem you noted, and actually, plays nice w/ node's
error-first callbacks:

```js
cb.apply(null, try JSON.parse('invalid'));
```
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to