You should do a post on this. I use JavaScript all the time but I'm seeing syntax I'm not familiar with. I feel like I'm peeking through a window from a room I didn't know I was stuck in.
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
😂 Both @c_basso and @fedyk solutions are coded in TS that can be the cause of your confusion @kevinleebuchan, here's a JS translation (guys correct me if I miss something)
/** Retrieves Ditto data */constgetDitto=async()=>{const[result,error]=awaitgo(fetch('https://pokeapi.co/api/v2/pokemon/ditto'));if(error)console.error(error);elseconsole.log(awaitresult.json());};awaitgetDitto();
Note that the return value of the go function is always an array with two positions, the first one intended to be a successful result, the second one reserved for the error so in case it succeeds it sends [result, null] and in case it fails it sends back [null, error], this may help to understanding:
Function for prevent using
try catch
in all async await callsand use it like this
You should do a post on this. I use JavaScript all the time but I'm seeing syntax I'm not familiar with. I feel like I'm peeking through a window from a room I didn't know I was stuck in.
😂 Both @c_basso and @fedyk solutions are coded in TS that can be the cause of your confusion @kevinleebuchan, here's a JS translation (guys correct me if I miss something)
Usage example:
Note that the return value of the
go
function is always an array with two positions, the first one intended to be a successful result, the second one reserved for the error so in case it succeeds it sends[result, null]
and in case it fails it sends back[null, error]
, this may help to understanding:Hope it helps! 😁
to
- funny name. I use similar helper, but I call itgo
. Here my recent post about it dev.to/fedyk/golang-errors-handing...