Easily give fetch() timeouts, type-safety, and better error handling

  Рет қаралды 57,380

Steve (Builder.io)

Steve (Builder.io)

3 ай бұрын

More in my full blog post: www.builder.io/blog/safe-data...
#javascript #typescript

Пікірлер: 22
@bubsztyn
@bubsztyn 3 ай бұрын
I recommend using unknown instead of any as a default result type. It will give you a warning if you forget to pass generic when trying to access result object fields
@TechDiffuse
@TechDiffuse 3 ай бұрын
That's super useful. Thanks for this,
@asadsalehumar1011
@asadsalehumar1011 3 ай бұрын
The abort signal is really helpful. However there is a cons when typecasting json response using "as" because you force TypeScript to believe it's that type, but in fact it could be any type. There are use cases though, like when we know for sure the response is of a certain type. Such thing can happen in thightly coupled system, such as: React Server Components. So, it's has pros and cons.
@funkdefied1
@funkdefied1 2 ай бұрын
I think “as” is appropriate here. The “as” assertion isn’t validated at runtime, so any weird response won’t immediately cause the program to crash like it would in a language with a stricter type system. Rather, it signals what the developer BELIEVES the response’s shape to be, given their knowledge of the API.
@ggoadmusic
@ggoadmusic 3 ай бұрын
I Like that!
@Noam-Bahar
@Noam-Bahar 3 ай бұрын
I love axios
@isaacjon
@isaacjon 3 ай бұрын
Why would you check for result.ok ? If it was not okay wouldn't it end up in the catch block?
@IllllIIllllI
@IllllIIllllI 3 ай бұрын
it only throws when there is a network error, but doesn't throw if the status code is 400 for example. result.ok is true when status code is in range 200-299
@isaacjon
@isaacjon 3 ай бұрын
@@IllllIIllllI so it would not be okay because it's not in the range of 200-299 right?
@arxci9402
@arxci9402 3 ай бұрын
Why would you time out a fetch call, lol? What if your users internet is so bad and it takes longer than the predetermined time limit you defined? Now, your user has absolutely zero chance in ever seeing your content instead of just waiting a little while linger
@asadsalehumar1011
@asadsalehumar1011 3 ай бұрын
There are use cases for this, especially in backend development
@asadsalehumar1011
@asadsalehumar1011 3 ай бұрын
For example, from my Node.js backend I might want to call another API service. But I definitely know that if it takes more than 30 seconds it's an error. So giving my fetch an AbortSignal is really useful.
@Erliortmejurur
@Erliortmejurur 3 ай бұрын
I have wasted so much time hahaha
@isaacjon
@isaacjon 3 ай бұрын
I can't understand so many things going on here
@zeeeeeman
@zeeeeeman 3 ай бұрын
you're using `as` casting again. That is NOT the way to do it.
@ramsey2155
@ramsey2155 3 ай бұрын
Or you could just properly use a typesafe rtc method
@TheScriptPunk
@TheScriptPunk 2 ай бұрын
Jesus....you made a class when you could just make a decorator function. Also, use a helper framework to operate the error check, and bubble through an error conrext handling component.
@PeterSahanaya
@PeterSahanaya 26 күн бұрын
Don't call Jesus name as a swear word
@David-gj6dc
@David-gj6dc 2 ай бұрын
"with full type checking" That's not how that works bud. Anytime you do network I/O without any runtime checks there is no type safety at all. This is JavaScript we're talking about. The only thing you get is autocomplete but you are basically lying to the compiler anytime you don't have runtime checks.
@_sleepy-
@_sleepy- 3 ай бұрын
Don’t use javascript for backend, solved like honestly you typed so much for something that should take so little
@kag3670
@kag3670 2 ай бұрын
AbortSignal.timeout() will throw error.name TimeoutError 😊
A Simpler Way to See Results
19:17
Logan Smith
Рет қаралды 102 М.
Why use Type and not Interface in TypeScript
14:12
ByteGrad
Рет қаралды 198 М.
لقد سرقت حلوى القطن بشكل خفي لأصنع مصاصة🤫😎
00:33
Cool Tool SHORTS Arabic
Рет қаралды 17 МЛН
Best Toilet Gadgets and #Hacks you must try!!💩💩
00:49
Poly Holy Yow
Рет қаралды 20 МЛН
БИМ БАМ БУМ💥
00:14
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 4 МЛН
Викторина от МАМЫ 🆘 | WICSUR #shorts
00:58
Бискас
Рет қаралды 3,7 МЛН
From React To HTMX
40:01
ThePrimeTime
Рет қаралды 314 М.
How To Build AI Products That Don't Flop
8:03
Steve (Builder.io)
Рет қаралды 11 М.
Production-Grade TypeScript by Johannes Schickling (Effect Days 2024)
26:58
Effect | TypeScript at Scale
Рет қаралды 15 М.
5 Async + Await Error Handling Strategies
18:11
Wes Bos
Рет қаралды 20 М.
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 466 М.
HTMX is amazing!
10:16
Maximilian Schwarzmüller
Рет қаралды 17 М.
Learn tRPC in 5 minutes
6:04
Matt Pocock
Рет қаралды 106 М.
Fetching Data Doesn't Get Better Than This
6:58
Josh tried coding
Рет қаралды 102 М.
Svelte 5 runes: what's the deal with getters and setters?
11:22
Rich Harris
Рет қаралды 42 М.
The cat chose the right one 🥰🥳😸
0:32
Ben Meryem
Рет қаралды 48 МЛН
БУКЕТ МЕЧТЫ НАСТОЯЩЕЙ СЛАДКОЕЖКИ😂😂😂
0:19
СЕМЬЯ СТАРОВОЙТОВЫХ 💖 Starovoitov.family
Рет қаралды 3,5 МЛН
Каха заблудился в горах
0:57
К-Media
Рет қаралды 9 МЛН