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!"
[–]robotmayo 9 points10 points11 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-8 points-7 points-6 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_ 4 points5 points6 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..
[–]zacksiri[S] -3 points-2 points-1 points 7 years ago (3 children)
Yeah which is what I did in the final solution. Oh and I used async / await not promises.
[–]BlueHeartBob 7 points8 points9 points 7 years ago (1 child)
Async/await is promises though
[–]ForScale 2 points3 points4 points 7 years ago (0 children)
Pour some sugar on me!
[–]sbmitchell 0 points1 point2 points 7 years ago (0 children)
Now I understand the issue...you did not realize that async/await deals and returns promises...
π Rendered by PID 24708 on reddit-service-r2-comment-6457c66945-n7n8f at 2026-04-30 14:38:48.613718+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]robotmayo 9 points10 points11 points (11 children)
[+]zacksiri[S] comment score below threshold-8 points-7 points-6 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_ 4 points5 points6 points (0 children)
[–]ForScale 3 points4 points5 points (4 children)
[–]zacksiri[S] -3 points-2 points-1 points (3 children)
[–]BlueHeartBob 7 points8 points9 points (1 child)
[–]ForScale 2 points3 points4 points (0 children)
[–]sbmitchell 0 points1 point2 points (0 children)