Back in August, I wrote an article in Brazilian-Portuguese explaining how I use async/await to isolate error handling.
Today I'll translate it to ...
For further actions, you may consider blocking this person and/or reporting abuse
Hi Eduardo, nice example but there are a couple of things that make me unconvinced this is the best approach in JavaScript:
.fetch()
or by.json()
and it's not going to be clear inside themain
functionhow are you rhymes? thanks for your comment!
not discussing the way the language handles side-effects (golang is built-in and javascript you need to wrap it in something like shown in this article), my personal experience is: it's just a matter of interface response pattern
i've being using it for node.js, typescript, flowtype, you name it... and it works pretty well, is just another pattern inside your codebase
there is nothing preventing you to massage these responses... you can easily branch it in the
catch
:in the same way you can inspect the error in the
main
function:use your imagination, but be consistent! :) ... is rare when we need to have these granular error handling in the front-end... in my perspective, your back-end should be handling it (checking for missing params, not found user, credentials, etc)...
in an node.js/express world, your error middleware would be responsible for these branches and the client response/rejection could be only one!
many thanks 👋
well thanks :D hope the same for you
Sure, I'm not against it, it's just that in a way you're limited to your perimeter of code, I wouldn't use it in a third party library. Most JS developers expect failure to be escalated to an exception so that if it's synchronous it can be caught and if it's async it can be handled. If suddenly you start returning two-valued arrays you're changing a silent contract. See how complicated it's going to be to change the way Go error handling is done. And Go is nowhere near as popular as JS.
It's not a bad idea inside an app though, as you say, if it's used consistently.
Thank you for your perspective!
I have a question, even though this blog post is about Node.js, I have a question about Go.
How can you mimic
Promise.all()
in Go? What would be equivalent code in Go?You probably need to use a WaitGroup.
This is the example you can find in the documentation link:
I was not aware of
WaitGroup
. Thanks!WaitGroup is awesome 🎉
funny enough, in Go is a little bit harder to do the exactly same!
i recommend you to go through two blog posts:
you will need to use channels/goroutines to achieve the same.. as said by rhymes in this thread, you will need to use WaitGroup!
there's a
merge
function in the documentation link of item 1 above, where using it, you can do something like this:running it will print:
i've been using it since I discovered it :)
I think you can also use errgroup to handle errors gracefully
This looks simple and powerful. Probably I'll have to dig deeper to understand it. Thanks for the resources.
Thanks for the post! I think this is a great pattern to follow.
I have one question left: is there a reason you return the error as first item and not the requested end-value?😇
thanks for your time Jannis! :)
my first approach was to mimic the Go way, but after it "clicked", I started using it in different ways!
🤗
Is there a reason though?🤔
the reason was to mimic Go... after switching between JavaScript and Go projects, I felt the need to try to write in a similar way in JavaScript :P
I got that - sorry if I wasn't clear enough, let me rephrase my question:
Is there a reason to go for a return that can be destructered as [err, value] over one that would be destructered with [value, err]?
I hope I made it understandable now.😇