More in my full blog post: www.builder.io/blog/safe-data... #javascript #typescript
Пікірлер: 22
@bubsztyn3 ай бұрын
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
@TechDiffuse3 ай бұрын
That's super useful. Thanks for this,
@asadsalehumar10113 ай бұрын
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.
@funkdefied12 ай бұрын
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.
@ggoadmusic3 ай бұрын
I Like that!
@Noam-Bahar3 ай бұрын
I love axios
@isaacjon3 ай бұрын
Why would you check for result.ok ? If it was not okay wouldn't it end up in the catch block?
@IllllIIllllI3 ай бұрын
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
@isaacjon3 ай бұрын
@@IllllIIllllI so it would not be okay because it's not in the range of 200-299 right?
@arxci94023 ай бұрын
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
@asadsalehumar10113 ай бұрын
There are use cases for this, especially in backend development
@asadsalehumar10113 ай бұрын
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.
@Erliortmejurur3 ай бұрын
I have wasted so much time hahaha
@isaacjon3 ай бұрын
I can't understand so many things going on here
@zeeeeeman3 ай бұрын
you're using `as` casting again. That is NOT the way to do it.
@ramsey21553 ай бұрын
Or you could just properly use a typesafe rtc method
@TheScriptPunk2 ай бұрын
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.
@PeterSahanaya26 күн бұрын
Don't call Jesus name as a swear word
@David-gj6dc2 ай бұрын
"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-3 ай бұрын
Don’t use javascript for backend, solved like honestly you typed so much for something that should take so little
@kag36702 ай бұрын
AbortSignal.timeout() will throw error.name TimeoutError 😊