You might be using `fetch` in JavaScript wrong...

preview_player
Показать описание
#js #javascript #webdev
Рекомендации по теме
Комментарии
Автор

I wouldn’t recommend resorting to a library for every small complication you face. If the simple “tacky” solution is enough, go for that. You’ll end up with a less complex codebase, ironically.

MrDgf
Автор

But I would say just write those bloated functions by hand as part of your project in a service and just them across which would remove the library dependency and also avoid the unnecessary code we might not use from that wretch library/ project

vishwanathbondugula
Автор

If you are using it in frontend give a shot to tansStack query v4(formally known as react query)

rohitkharche
Автор

to everyone talking about Axios, I will suggest you looking in redaxios (which he mentioned in a future video). Redaxios is just Axios but it is only 800b compared to Axios at 11kb.
Please stop talking about "JUST USE AXIOS", let the developer pick what is right for their usage.

Dev-Siri
Автор

This is one of the problems with the front end world, to many js devs resort to some random dependency for small code changes they don’t either know how or want to take the time to create introducing a dependency web that over time becomes out of control

LiquaFoo
Автор

I'd really use it in a trycatch and use the callback

heitormbonfim
Автор

What about using axios library instead?

someshrandive
Автор

no way if my backend will suddenly send me random error.

hsmtrue
Автор

Do it without wretch and without if block and without case block.

thedelanyo
Автор

Lol naw dude. Just use ECMAscript based methods and objects.

m
Автор

Try/catch with asynchrony/await is your friend

cas
Автор

Taking on more maintenance headaches because you don't like a switch statement is definitely not the answer.

vmaxxk
Автор

I completely hate these ridiculous APIs for HTTP. It should not be 30 lines of code with cancellation tokens and timeouts and ridiculous error handling and no easy way to know without a day of research what the errors you might even get back are. I fix this by making a message passing system and microservices where I simply ask an HTTP expert to get me what I want and it handles everything the same way for HTTP that it would for Mongo queries or any other function that might throw or fail.

supercompooper
Автор

As long as the API I'm hitting sends JSON and not HTTP 204, I tend to use .then(res => Promise.all([res, res.json()])).then(([res, body])=>{...}). This allows for handling the status code and the body.

stcm
Автор

If status code is 300+ - try to parse the data as json anyway. In some cases, the data has a custom {error: message} with better details. Otherwise I throw the status code and status message if parsing fails, or it doesn’t have an error message on the JSON.

LewisMoten
Автор

I want to build out a solution for requests. Something like Wretch is a nice solution, but I'm wondering if a dependency is the way to go for something like this. Feels like it should be something that can be easily updated/changed.

incarnateTheGreat
Автор

No offense but you should avoid using/depending on packages as much as possible. Especially if it is only a small problem, cuz it's not good for a scallable project. Imagine maintaining hundreds of dependencies every single times.

Conetall
Автор

This simple wrapper you can easily make yourself without depending on an 3rd party library. 😅

profoundgames_
Автор

I'm sorry but this is horrible advice. He called the switch statement bloated, and then recommends using a library instead? Libraries are the reason software becomes bloated. Keep it simple, don't be afraid to do the ugly solution.

owenpalmer
Автор

javascript programmer dont use a trillion libraries for every small thing challenge

ikqqslate_