I'm Calin Baenen – AKA KattyTheEnby – a programmer born October 30th, 2006.
I love programming, it has been my passion since I was a kid, and will forever be my passion.
I'm Calin Baenen – AKA KattyTheEnby – a programmer born October 30th, 2006.
I love programming, it has been my passion since I was a kid, and will forever be my passion.
I'm Calin Baenen – AKA KattyTheEnby – a programmer born October 30th, 2006.
I love programming, it has been my passion since I was a kid, and will forever be my passion.
I'm Calin Baenen – AKA KattyTheEnby – a programmer born October 30th, 2006.
I love programming, it has been my passion since I was a kid, and will forever be my passion.
The thing is: a good bunch of developers is not prepared to "just use" Rust. The learning curve for Rust is the same as its error handling: exceptional.
But once learned and added to your developer toolchain, it is unlikely that you come back to this article in 2028 and push the next programming language, as you might have learned on the way that not every programming language is suitable for all use cases.
I prefer array because if we use object, destructuring will be an issue if there is more than one block in same level. Also we need to be aware of the property name.
Graduated in Digital Media M.Sc. now developing the next generation of educational software. Since a while I develop full stack in Javascript using Meteor. Love fitness and Muay Thai after work.
Property name can be set by convention across your whole project. Let's say { error, result } which is to me most verbose and would not lead to accidental access compared to array indices
constresponseOne=awaitmethodOne();if(responseOne.error)returnconstresponseTwo=awaitmethodTwo();if(responseTwo.error)return// of if you want destructuringconst{error:methodOneError,result:resultOne}=awaitmethodOne();if(methodOneError)returnconst{error:methodTwoError,result:restultTwo}=awaitmethodTwo();if(methodTwoError)return
Im not saying the array descructuring is worse, its just a prefference
I'm not a huge fan of this pattern.
The idea of explicitly returning errors rather than throwing unchecked errors is great, but the proposed implementation is flawed - we need to always check the elements of the tuple before using the value to see if there is an error which will become error-prone rather quickly.
Fortunately, this is an already solved problem, known in other langues as Either or Result type, in which all of these ifs are abstracted away in form of map and flatMap / chain operations.
Potential implementation: gigobyte.github.io/purify/adts/Eit...
I am a full stack engineer and have been coding since I was ~12. My primary focus is to create beautiful, stunning experiences with the best designers available. In tandem, I push to keep perf. bottle
You can actually turn this into a utility function, like so:
You could also use an object like Brendan said.
I would wrap it in a higher order function though, something that takes an async function and returns a new async function with the added catch logic
Something like this, I guess:
Yeah. That can be way of implementing it.
Yeah. Utility function can be very helpful.
Better yet, just use GoLang.
Good idea. 😉
Of course it is! Everyone knows Go is the best language objectively.I've reconsidered. . .
Thinking back, I spoke very unwise words.
It should be "Better yet, just use Rust." — Rust's error handling is exceptional. :þ
The thing is: a good bunch of developers is not prepared to "just use" Rust. The learning curve for Rust is the same as its error handling: exceptional.
But once learned and added to your developer toolchain, it is unlikely that you come back to this article in 2028 and push the next programming language, as you might have learned on the way that not every programming language is suitable for all use cases.
I added a callback
onLoadingso I can do somenthing while function is running.const { data, error } = await promiseHandler(callbackFn, { onLoading: value => console.log('is loading?', value) })👨🏼💻
Nice, but I would personally use an object instead of the array as a return type
I prefer array because if we use object, destructuring will be an issue if there is more than one block in same level. Also we need to be aware of the property name.
Property name can be set by convention across your whole project. Let's say
{ error, result }which is to me most verbose and would not lead to accidental access compared to array indicesYeah, that is right. But in case of multiple blocks in the same scope it can create issue, as you will not be able de-structure it like this
But in case of array, you can use other variable name in the second method call.
Feel free to correct me, if I am missing something.
a few ways you could avoid this:
Im not saying the array descructuring is worse, its just a prefference
You can rename the variable when destructuring an object.
I'm not a huge fan of this pattern.
The idea of explicitly returning errors rather than throwing unchecked errors is great, but the proposed implementation is flawed - we need to always check the elements of the tuple before using the value to see if there is an error which will become error-prone rather quickly.
Fortunately, this is an already solved problem, known in other langues as
EitherorResulttype, in which all of theseifsare abstracted away in form ofmapandflatMap/chainoperations.Potential implementation: gigobyte.github.io/purify/adts/Eit...
Got your point. After all it's all about own preference.
I saw this in a Fireship video, ditto 👀
Is it? Need to check it.
I saw this in a 3 years old article.
Can just do
if (error)though.Yeah, we can do that. Sometimes I just like to check explicitly.
in typescript
Nice idea. Thanks for sharing :)
I'm glad that you liked it. 😊
Got it. Thank you for sharing that.
Sure :)