FAQ: Requests II - fetch() POST Requests II

This community-built FAQ covers the “fetch() POST Requests II” exercise from the lesson “Requests II”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise fetch() POST Requests II

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I am curious why, if I wanted to enclose the ‘throw’ statement in an else{ } block, it creates an error ( as opposed to just using throw in the function block. Thanks!

Example:

.then( (response) => {
if(response.ok){
return response.json();
}else{
throw new Error(‘Request failed!’); // THIS CAUSES AN ERROR
}

1 Like

I am wondering why the 2nd .then() is attached to the fetch()? The lesson says " The purpose of this step is to view the JSON that was returned from the previous .then() ."

However the first .then() already returns “return response.json();” SO i feel this is redundant…Am I right? We dont need that extra .then() as we already got the response.

9 Likes

I was wondering the same thing

Me three.

Why not just leave the second .then() off?

Because a .then() has two parameters, one for success and one for failure.

Why are we checking for failure twice? What are they checking for, and why do we need to check for an error within the success callback?

I think it might have something to do with that content in that article provided at the beginning regarding fetch() method. If we dig deeper.
From MDN website: "… The fetch specification differs from jQuery.ajax() in three main ways:

  • The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500. Instead, it will resolve normally (with ok status set to false), and it will only reject on network failure or if anything prevented the request from completing. …"
    https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

wikipedia:
"404 Not Found

The requested resource could not be found but may be available in the future. Subsequent requests by the client are permissible."

" 500 Internal Server Error

A generic error message, given when an unexpected condition was encountered and no more specific message is suitable.[63]"

3 Likes

I can’t be the only one that thinks the way this module has been taught has been absolute garbage no?

6 Likes

No, I do not agree.
If you want to learn programming languages to a substantial level, than you will have to read through the documents. There is literally no way around it.
These courses are great because they give you an opportunity to see the code being used first hand, and allows students like us to develop some perspective and experience with manipulating simple codes.

1 Like

I know this is an old thread now, but it was my question too and I don’t understand how this is the answer. The first .then() function has two parameters, and then there’s a second then() which returns a variable we haven’t seen before. It’s hard to see what this is doing.

The second .then() is needed because response.json() is also asynchronous and returns a Promise rather than the actual data.

It’s kind of tough concept to wrap your head around if you don’t already understand how Promises work. They have a course on async stuff, haven’t actually looked at it, but would be helpful as a prereq to these fetch lessons:

https://www.codecademy.com/learn/asynchronous-javascript

It’s not clear to me how the second then() call adds anything to the code. It literally returns the argument it gets (jsonResponse) as is. What’s the point of a function that returns its only argument without doing anything else?
I thought a then() method was supposed to take a Promise and then return an actual response object?