I'm a Sr. Software Engineer at Flashpoint. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
Is there a reason not to use a Try/Catch block? That seems a little more explicit, but I'm not sure, maybe that's not idiomatic for JavaScript. I haven't done enough big JavaScript to know. At least in Python, it seems like it would work well enough to:
So yeah, maybe we do need this new operator, although I really am worried about handling probably-invalid data structures. I mean, isn't that an indicator we should probably write our code a different way? Maybe we should, for example, use another function to handle that specific input, which should be checked before that? I don't know, just speculating.
yes, like I said model your data right and things get more robust.
The promis catching is nice and I prefer it, but sometimes await is nicer in a specific case and you have to try-catch, I guess ?. would be simpler then.
I'm a Sr. Software Engineer at Flashpoint. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
Try-Catch aren't popular in JS because engines can't optimize the code well, or something of this sort that I read somewhere a while ago. I use Try-Catch with promises quite often.
Is there a reason not to use a Try/Catch block? That seems a little more explicit, but I'm not sure, maybe that's not idiomatic for JavaScript. I haven't done enough big JavaScript to know. At least in Python, it seems like it would work well enough to:
I guess using a block is maybe not at shwoopy as one quick line, like above.
What about...
Easy as that... cough
I mean, we could also do this (to get rid of the IIFE), but that's unfunctional, and I don't wanna use a
let
tbh.Another option would probably be this, but ew, async where we don't need it...
So yeah, maybe we do need this new operator, although I really am worried about handling probably-invalid data structures. I mean, isn't that an indicator we should probably write our code a different way? Maybe we should, for example, use another function to handle that specific input, which should be checked before that? I don't know, just speculating.
yes, like I said model your data right and things get more robust.
The promis catching is nice and I prefer it, but sometimes await is nicer in a specific case and you have to try-catch, I guess ?. would be simpler then.
You're totally right. Somehow JS devs aren't too fond of try-catch, guess it's like you said: too explicit?
Maybe it has also to do with the async nature of JS code, you simply couldn't catch them callbacks, so many people didn't use it too often.
Ooh that makes sense. Cool, thanks!
I have been told "Avoid using exceptions for control flow". If you know something might throw an exception and you can check for it, do that instead.
A try block is also greedy and you may end up swallowing more than what you were hoping for, which can be a PITA for chasing down odd errors.
Yeh, in statically typed languages you can at least easily filter what you wanna catch :/
isn't that why you now have promises and the .catch() there?
sure, but you're not always inside a promise
Try-Catch aren't popular in JS because engines can't optimize the code well, or something of this sort that I read somewhere a while ago. I use Try-Catch with promises quite often.
I read that's the case for many languages.
Exceptions are generally really expensive. Actual usage of them should be an exceptional case.