Read more in my full article: www.builder.io/blog/safe-data... #javascript #typescript
Пікірлер: 41
@mettle_x3 ай бұрын
The last part is not "type-safe" unless you have a runtime response validator with something like Zod.
@andreujuanc3 ай бұрын
100%
@newfluency3 ай бұрын
Yes, the type would be just inferred in his code. I guess he is assuming we are fetching from our own API and not an external API, in which case it would be valid to infer rather than validate the response. Or at least so I think.
@zeeeeeman3 ай бұрын
like @newfluency has stated - using the `as` cast escape hatch because result.json() is any.
@Tommy-ev6gv3 ай бұрын
I'm happy to see your channel popup on my feed again, always enjoy your content. Hope everything is great!
@Razunter3 ай бұрын
Wretch library is nice Fetch wrapper
@fueledbycoffee5833 ай бұрын
i actually think fetch is really ok, no need for a wrapper
@StCost3 ай бұрын
Nice one, will simplify my fetches all over the project
@mattiarasulo99343 ай бұрын
Hey this one I knew and used already all the time! I love throwing my own errors!
@irfanmohammad72693 ай бұрын
nice content
@TheTwober3 ай бұрын
A few more lines and you advance to a Java programmer. :)
@PraiseYeezus3 ай бұрын
OOP is great and reliable until you try to choke me to death with dependency injection a forest of inheritance trees
@nested93013 ай бұрын
To be honest a rather be a java programmer if it's more organised like this
@TheTwober3 ай бұрын
@@nested9301 httpClient.sendAsync(httpRequest, BodyHandlers.ofString()) .thenApply(this::processResponse) .exceptionally(this::handleException); And in the handleException you then get a HttpConnectTimeoutException as input.
@tigrezno26263 ай бұрын
that's why I still use axios, it just works
@mattp01233 ай бұрын
Nice video! Is there a better way to handle network error?
@bring-shrubbery3 ай бұрын
just use effect
@dolevklefner8113 ай бұрын
You should check Effect it is more robust and simplifies the fetch handling
@tomm57653 ай бұрын
Effect seems to be getting some attention at the moment, are you using it in a project?
@chinmayghule82723 ай бұрын
What's the difference between using Effect and using Tanstack Query?
@skyhigheagleer62 ай бұрын
It does neither. No one is refactoring for Effect
@invinciblemode3 ай бұрын
Need to validate the fetch response too instead of just casting as ResponseType
@enyelsequeira36193 ай бұрын
lol I had to implement this and created like my own axios around fetch because next js doesn’t play well with axios
@dokkenrox3 ай бұрын
Might as well just use an existing library like Axios that already has all these features and is well tested.
@reaper843 ай бұрын
I wonder how many developers have wrapped fetch like this... I sure have done that myself.😅
@rand0mtv6603 ай бұрын
In my opinion fetch is unusable without a wrapper. At least if you are working on an actual project, not just some tech demo. I mean you can use it without a wrapper, but after some time you'll hate yourself for doing it that way.
@pajeetsingh3 ай бұрын
why are you awaiting the fetch call? don't you want it to be asynchronous?
@lottexy3 ай бұрын
In real world scenarios, you want to await calls before proceeding. IE: you submitted data, wait for submission request to complete before returning to list of submissions.
@recursiv3 ай бұрын
Await is what makes it async...
@quadra54433 ай бұрын
dude you look like the benji dude from "You"
@gabrieldelosrios8113 ай бұрын
Lol, how did you even made that connection 🤣
@joeys.42683 ай бұрын
I always like to do throw { status: "error", msg: ..., code: ..., etc } Instead of throw new Error because it allows me to throw anything I want
@rand0mtv6603 ай бұрын
But then you cannot check if an error is an instance of a specific error class since you just threw a random object basically. Error in catch block might be some other runtime error you didn't throw manually. You can of course create a TypeScript type guard that will check all these properties exist on the error object and assert a specific type for that error variable if they do exist, but at that point why not just use a custom class and use JS runtime features for that check.
@guytonedhai3 ай бұрын
Golang laughing in corner
@jalbers31503 ай бұрын
I love error handling in golang. It’s so easy and simple to get exactly what you want.
@bg2junge3 ай бұрын
Don't you want to throw a HTTPError(???) in the try block?
@Steve87083 ай бұрын
yeah, I neglected to do that at first, but fixed it in the “simpleFetch” abstraction step 😅