A Better Way to fetch() in JS

  Рет қаралды 11,287

Steve (Builder.io)

Steve (Builder.io)

3 ай бұрын

Read more in my full article: www.builder.io/blog/safe-data...
#javascript #typescript

Пікірлер: 41
@mettle_x
@mettle_x 3 ай бұрын
The last part is not "type-safe" unless you have a runtime response validator with something like Zod.
@andreujuanc
@andreujuanc 3 ай бұрын
100%
@newfluency
@newfluency 3 ай бұрын
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.
@zeeeeeman
@zeeeeeman 3 ай бұрын
like @newfluency has stated - using the `as` cast escape hatch because result.json() is any.
@Tommy-ev6gv
@Tommy-ev6gv 3 ай бұрын
I'm happy to see your channel popup on my feed again, always enjoy your content. Hope everything is great!
@Razunter
@Razunter 3 ай бұрын
Wretch library is nice Fetch wrapper
@fueledbycoffee583
@fueledbycoffee583 3 ай бұрын
i actually think fetch is really ok, no need for a wrapper
@StCost
@StCost 3 ай бұрын
Nice one, will simplify my fetches all over the project
@mattiarasulo9934
@mattiarasulo9934 3 ай бұрын
Hey this one I knew and used already all the time! I love throwing my own errors!
@irfanmohammad7269
@irfanmohammad7269 3 ай бұрын
nice content
@TheTwober
@TheTwober 3 ай бұрын
A few more lines and you advance to a Java programmer. :)
@PraiseYeezus
@PraiseYeezus 3 ай бұрын
OOP is great and reliable until you try to choke me to death with dependency injection a forest of inheritance trees
@nested9301
@nested9301 3 ай бұрын
To be honest a rather be a java programmer if it's more organised like this
@TheTwober
@TheTwober 3 ай бұрын
@@nested9301 httpClient.sendAsync(httpRequest, BodyHandlers.ofString()) .thenApply(this::processResponse) .exceptionally(this::handleException); And in the handleException you then get a HttpConnectTimeoutException as input.
@tigrezno2626
@tigrezno2626 3 ай бұрын
that's why I still use axios, it just works
@mattp0123
@mattp0123 3 ай бұрын
Nice video! Is there a better way to handle network error?
@bring-shrubbery
@bring-shrubbery 3 ай бұрын
just use effect
@dolevklefner811
@dolevklefner811 3 ай бұрын
You should check Effect it is more robust and simplifies the fetch handling
@tomm5765
@tomm5765 3 ай бұрын
Effect seems to be getting some attention at the moment, are you using it in a project?
@chinmayghule8272
@chinmayghule8272 3 ай бұрын
What's the difference between using Effect and using Tanstack Query?
@skyhigheagleer6
@skyhigheagleer6 2 ай бұрын
It does neither. No one is refactoring for Effect
@invinciblemode
@invinciblemode 3 ай бұрын
Need to validate the fetch response too instead of just casting as ResponseType
@enyelsequeira3619
@enyelsequeira3619 3 ай бұрын
lol I had to implement this and created like my own axios around fetch because next js doesn’t play well with axios
@dokkenrox
@dokkenrox 3 ай бұрын
Might as well just use an existing library like Axios that already has all these features and is well tested.
@reaper84
@reaper84 3 ай бұрын
I wonder how many developers have wrapped fetch like this... I sure have done that myself.😅
@rand0mtv660
@rand0mtv660 3 ай бұрын
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.
@pajeetsingh
@pajeetsingh 3 ай бұрын
why are you awaiting the fetch call? don't you want it to be asynchronous?
@lottexy
@lottexy 3 ай бұрын
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.
@recursiv
@recursiv 3 ай бұрын
Await is what makes it async...
@quadra5443
@quadra5443 3 ай бұрын
dude you look like the benji dude from "You"
@gabrieldelosrios811
@gabrieldelosrios811 3 ай бұрын
Lol, how did you even made that connection 🤣
@joeys.4268
@joeys.4268 3 ай бұрын
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
@rand0mtv660
@rand0mtv660 3 ай бұрын
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.
@guytonedhai
@guytonedhai 3 ай бұрын
Golang laughing in corner
@jalbers3150
@jalbers3150 3 ай бұрын
I love error handling in golang. It’s so easy and simple to get exactly what you want.
@bg2junge
@bg2junge 3 ай бұрын
Don't you want to throw a HTTPError(???) in the try block?
@Steve8708
@Steve8708 3 ай бұрын
yeah, I neglected to do that at first, but fixed it in the “simpleFetch” abstraction step 😅
Building Fluent Interfaces in TypeScript
16:15
Andrew Burgess
Рет қаралды 13 М.
Learn Why JavaScript Frameworks Love Signals By Implementing Them
20:15
Amazing weight loss transformation !! 😱😱
00:24
Tibo InShape
Рет қаралды 62 МЛН
Finally Fix Your Issues With JS/React Memory Management 😤
20:13
Jack Herrington
Рет қаралды 83 М.
Pretty much every website uses the wrong font size…
15:33
Theo - t3․gg
Рет қаралды 66 М.
The Most Important Skill You Never Learned
34:56
Web Dev Simplified
Рет қаралды 183 М.
9 JavaScript Features You’ve Never Used
19:36
Conner Ardman
Рет қаралды 27 М.
7 Awesome TypeScript Types You Should Know
8:57
Josh tried coding
Рет қаралды 80 М.
How principled coders outperform the competition
11:11
Coderized
Рет қаралды 1,6 МЛН
Svelte 5's Secret Weapon: Classes + Context
18:14
Huntabyte
Рет қаралды 14 М.
Turns out REST APIs weren't the answer (and that's OK!)
10:38
Dylan Beattie
Рет қаралды 122 М.
The New JS Features Coming Soon (I'm so hyped)
39:03
Theo - t3․gg
Рет қаралды 83 М.
5 Design Patterns That Are ACTUALLY Used By Developers
9:27
Alex Hyett
Рет қаралды 232 М.
Мыла наелся
0:21
Pavlov_family_
Рет қаралды 4,6 МЛН
ХОТЕЛ ПОТОПИТЬ ДЖЕКА, НО НЕ ВЫШЛО
0:51
Tasty Series
Рет қаралды 823 М.
Waka waka 🤣 Which video is the best 1,2,3,4? 🤩
0:13
Adani Family
Рет қаралды 7 МЛН
Гаджеты вредно!!! ☎️😳😡 #funny #comedy
0:26
Fast Family LIFE
Рет қаралды 750 М.
ПИЩЕВОЙ ВАНДАЛ НАКАЗАН
0:20
МАКАРОН
Рет қаралды 2,6 МЛН