tc39 / proposal-optional-chaining

https://tc39.github.io/proposal-optional-chaining/
4.94k stars 75 forks source link

Relation to `do` expressions #35

Closed graingert closed 6 years ago

graingert commented 7 years ago

It seems that "do" expressions can do optional chaining something a bit like optional chaining in a simpler way:

const value = do { try { ham.spam.eggs().bar } catch (e) { } } || 'foo';

or if do adds support for catch expressions:

const value = do { try { ham.spam.eggs().bar } catch { 'foo' } };
ljharb commented 7 years ago

In no way is that simpler; try/catch there would swallow other errors (like in eggs() e.g.). Optional chaining isn't for error suppression; it's to avoid unsafely navigating into null and undefined.

graingert commented 7 years ago

@ljharb updated.

ljharb commented 7 years ago

If the implementation of eggs is () => { throw new Error(); }, then optional chaining would allow it to be thrown while your example would not.

I'm not sure what else there is to discuss on this issue.

claudepache commented 7 years ago

The optional chaining operator ?. does a targeted check on the value of its LHS. The try/catch construct swallows all errors from anywhere in the try clause, included those thrown by functions invoked inside the clause. You are free to use the latter construct when you mean the former, but your code will be more difficult to debug.

claudepache commented 6 years ago

Closing this issue while doing housekeeping.