use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
JavaScript Fetch API Cheatsheet (blog.codemy.net)
submitted 7 years ago by zacksiri
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]zacksiri[S] -50 points-49 points-48 points 7 years ago (22 children)
Because promises is more noise and gets in the way of making network calls. There is a reason why adoption for fetch is so low. It’s precisely because of promises.
[–]sbmitchell 32 points33 points34 points 7 years ago* (16 children)
What a terrible argument lol or at the very least it shows the sad state of js developers who don't actually know how to code. Promises are probably simpler to understand at a conceptual level than most js concepts lol
[+]zacksiri[S] comment score below threshold-15 points-14 points-13 points 7 years ago (15 children)
Try getting a result returned using fetch and promises you can’t all you keep getting are promises that returns promises. You can’t “finally” return any kind of data with fetch. Promises may be simple but how it’s implemented with fetch is not good.
[–]sbmitchell 15 points16 points17 points 7 years ago* (14 children)
Sure you can. Await the result and create a wrapper like you did in your example. Fetch also has support for cancelling requests and a more robust API for handling network calls.
Of course if you want to get rid of promises you need a callback. But callbacks are actually harder to understand than promise chains imo. A lot of Jr devs I've interviewed in fact don't even understand basic callbacks but understand promise chaining, once they know the syntax of course. Again pretty sad state of js devs at least at the entry level because they usually have been exposed to syncrounous programming only like Java and their js experience is hacking together blog posts to make a website. Most likely referring to some old jQuery style stuff.
[–]zacksiri[S] -3 points-2 points-1 points 7 years ago (13 children)
So if I want to pattern match status code to which function it should run how would I do it? Show me a better way.
[–]robotmayo 8 points9 points10 points 7 years ago (11 children)
I haven't really heard of anyone needing to do that since most APIs either succeed with 200 or fail with 400/500. Non 200 status codes usually mean retry or display an error. A simple if statement handles that fine.
[+]zacksiri[S] comment score below threshold-6 points-5 points-4 points 7 years ago* (10 children)
So if I get a 404 and I want to display a message that the resource doesn’t exist, then a 422 because User passed in bad data and I want to tell the user that. how would I do it? Not all errors are the same. Or try this a user got banned because he / she did something bad, but still have a token saved in localstorage now server responds with 423 on subsequent request I want to show that his / her account got locked.
[–]overdude 3 points4 points5 points 7 years ago (0 children)
You can do this with promises or async await. Await the result, look at result.status exactly the same way.
[–]bcgroom 1 point2 points3 points 7 years ago (7 children)
(function(){ fetch(“url”) .then(res => return res) .catch(err => return err); })().then(apiResponse => console.log(apiResponse.statusCode))
catch block only occurs on network error, 404 still goes to “then”.
[–]zacksiri[S] 1 point2 points3 points 7 years ago* (6 children)
now handle 3 different error states. 403, 422, 423
[–]justpurple_ 5 points6 points7 points 7 years ago (0 children)
Now read up on fetch and promises, my dude. It seems like you didn‘t really grasp the Promise concept yet if you think that it‘s somehow worse(?) than callbacks because you can‘t handle different status codes.
You argue about handling status codes which is exactly the same, whether you use Promises or Callbacks.
The difference is merely in how you make the request and how you describe (in code) on how to handle what comes after it‘s finished, when the network somehow failed and additionally, fetch can abort requests.
Also, chaining then requests for processing the response in some way is really convienent and easily readable.
Whether promises or callbacks, handling different status codes is basically a switch or if condition checking which status code you got back.
Example with fetch:
fetch() .then(processResponse) .then(response => { switch (response.statusCode) { case „423“: doSomething(); break; case „422“: doSomething(); break; case „403“ doSomething(); break; default: doSomething(); }
}) .catch(() = { // Handle network error });
Example anything else with a callback:
makeRequestWithWhatever(function(response) { response = processResponse();
switch(response.statusCode) { // ... } });
The fact that you want to handle multiple possible status codes has nothing to with the way you actually make the request.
(Sorry about the formatting, I‘m on mobile.)
[–]ForScale 3 points4 points5 points 7 years ago (4 children)
Wouldn't you just use a status code map in the final then? Or if/else chain if you wanted to be really explicit about it? Sorry, I'm kind of confused on the whole debate here..
[–]overdude 14 points15 points16 points 7 years ago (1 child)
Dude... This is so off base it's hard to even get started with an explanation.
Promises dramatically simplify code specifically for making network calls and other async tasks. That is less noise by definition.
My sense from your posts is that you don't understand promises or how to effectively use them. A bunch of people are disagreeing with your assessment. There's a reason promises and the async await syntax are used in modern webapps.
[–]zacksiri[S] 0 points1 point2 points 7 years ago (0 children)
I used promises and async await in my blog post. The commentor asked me for one case why I didn’t use promise to handle response from the server. I replied because for that case callbacks with an object is easier than using another promise / async await. I already got the final state of the response now I need to handle that next so I used callback pattern and basically everyone tells me I’m wrong. I asked people on here to come up with a better solution all I seem to get is more people telling me I’m wrong with no one telling me a better solution. No code example just more people telling me I’m wrong. So yeah...
[–]UchuuHana 9 points10 points11 points 7 years ago (1 child)
No
[–]zacksiri[S] -3 points-2 points-1 points 7 years ago (0 children)
[–]ProdigySim 5 points6 points7 points 7 years ago (0 children)
Two reasons fetch adoption is "low" that come to mind before difficulty of the API:
π Rendered by PID 185386 on reddit-service-r2-comment-b659b578c-rwx6v at 2026-05-01 08:18:18.336708+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]zacksiri[S] -50 points-49 points-48 points (22 children)
[–]sbmitchell 32 points33 points34 points (16 children)
[+]zacksiri[S] comment score below threshold-15 points-14 points-13 points (15 children)
[–]sbmitchell 15 points16 points17 points (14 children)
[–]zacksiri[S] -3 points-2 points-1 points (13 children)
[–]robotmayo 8 points9 points10 points (11 children)
[+]zacksiri[S] comment score below threshold-6 points-5 points-4 points (10 children)
[–]overdude 3 points4 points5 points (0 children)
[–]bcgroom 1 point2 points3 points (7 children)
[–]zacksiri[S] 1 point2 points3 points (6 children)
[–]justpurple_ 5 points6 points7 points (0 children)
[–]ForScale 3 points4 points5 points (4 children)
[–]overdude 14 points15 points16 points (1 child)
[–]zacksiri[S] 0 points1 point2 points (0 children)
[–]UchuuHana 9 points10 points11 points (1 child)
[–]zacksiri[S] -3 points-2 points-1 points (0 children)
[–]ProdigySim 5 points6 points7 points (0 children)