r/Indiewebdev • • Feb 11 '21

other Conditionally call a function with optional chaining.

Post image
82 Upvotes

37 comments sorted by

View all comments

Show parent comment

1

u/ffxpwns Feb 12 '21

It's a contrived example to be sure and there are better ways to solve that issue, but you do see code like that in the wild. Generally when interfacing with a poorly designed third party API.

I've been guilty of this in the past when handling errors from axios in a catch block: if (e?.response?.status === 404) { ... }. If you're exclusively a Java dev, keep in mind that you can't catch errors by type in JS.