Closed graingert closed 6 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.
@ljharb updated.
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.
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.
Closing this issue while doing housekeeping.
It seems that "do" expressions can do
optional chainingsomething a bit like optional chaining in a simpler way:or if do adds support for catch expressions: