Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Asynchronous Programming with JavaScript Understanding Promises Using Fetch

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree
karan Badhwar
Web Development Techdegree Student 13,921 Points

Somebody Please help

I need to finish this lesson now, I spent 6 hours, watched the same video over a dozen time. I have couple of questions,

1)- response.json() returns a javascript Object right, as mentioned in the MDN, then why @Guil said we gonna parse the response to JSON, or if we are parsing it to JSON, then how the heck are we parsing it back to an object ?

2) - why do we need to return an object on the last .then on getProfiles?, we are already returning the last .then() in an object as we already know the .then() method returns a promise object, then why are putting that in an object?

3)-.catch(err => console.log('Error found', err)) , how the err is getting a reject message from? and the 'Error found ' message does it have anything to do with the parameter?

1 Answer

Caleb Kemp
Caleb Kemp
12,676 Points

:smile: I think the main thing that is giving you difficulty is that you are missing the idea of the lesson. response.json() works, and there is nothing wrong with it. However, the teacher wanted to show us another (potentially better) way of handling that functionality using the Fetch API. Most of the changes needed in the code are because the Fetch API functions a little differently than response.json(). That being said,

  1. You're absolutely right, "response.json()" does return a JavaScript Object. However, in the video, we are being asked to replace "response.json()" with the calls to the "fetch" API. The "fetch" API returns "promises" not JavaScript Objects, hence the need to parse to JSON.

  2. Almost the same thing, the "response.json()" call got replaced by "fetch". Since "fetch" returns "promises" not JavaScript Objects, the promises have to be parsed to JSON.

  3. This concept may be a little more difficult to understand. The catch function only gets triggered if an error occurs which it will pass to the catch block. Do you see where it says the first "err" in catch(err => console.log('Error found', err))? That is the variable name that the error is getting passed to. So, when the rest of the function => says console.log('Error found', err)), it will print the message "Error found" + the message stored in the err variable.

Don't worry about it, everyone has concepts that give them trouble sometimes. Hope that helps.

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree
karan Badhwar
Web Development Techdegree Student 13,921 Points

Hey Caleb Kemp , thanks again for the help, I really appreciate it, actually the problem is the Lessons are to crisp, for a newbie to understand new terms, I know I am weird, I go way to into details, but I cannot help myself, that's why i force myself to understand the concept in which I get stuck, Moreover the meaning of lesson gets lost when I cannot understand what they are doing and then I have to find that out first before understanding the video, been on this video for 3 days now and now I don't feel like watching it again coz I am scared I might end up staying even longer to understand the concept and I really appreciate your help you have been helping me a lot BDW! and really sorry for being such a pain

One thing I still did not get the second questions,

when guil tried to add the ISS text from craft it returns the an object from inside the .then() i am not getting that.. I am talking about this code

 const profiles = json.people.map( person => { 
                        const craft = people.craft;
                            return fetch(wikiUrl + person.name)
                             .then(response => response.json())
                              .then( profile => { return {...profile, craft}})
                               .catch(err => 'Error fetching wiki..', err);

  });
})
Caleb Kemp
Caleb Kemp
12,676 Points

Well, I'm glad to see you taking the effort to try and understand fully the concepts presented. I also feel compelled to understand the inner workings of what I'm working on, like an itch in my brain that needs to be scratched. While it can often be very useful, I have also found that unless managed, that same need for information can bring progress in a given area to almost a standstill, with few results to show. So, I have found sometimes (even though maddening), I compel myself to push on. Some concepts just take our brains time to process. Sometimes the very next lesson might lead you to the piece you're missing, or maybe (as I have at times) you'll come across someone who can explain it in a way that makes more sense to you. I guess I should finally get to the actual question :smile:

How come the .then returns an object instead of a promise? Unless I'm mistaken, this is because Guil used the return keyword, and what he returned was an object. {...profile, craft} (which were objects).

Great job putting in the effort to study, I hope this was helpful (even if just a tiny bit :wink: )

PS Glad you found the previous post helpful :grinning:

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree
karan Badhwar
Web Development Techdegree Student 13,921 Points

Caleb Kemp, well for few posts the reason is you that I am able to understand the concepts not the teacher himself lol๐Ÿคฃ, but again thanks so much and yes it sometime gets soo frustrating to go over same thing coz I wanted to finish this chapter from long time and it stretched a lot, but it gets hard as well to move forward to next lesson and I am sure on every lesson I get stuck, because I found the info a bit less then it should be so I avoid pilling up more questions on my previous questions. And yes I understood it now, thanks a lot again, please don't mind if I tag you in my future questions to get the early answer from you please, sorry and thanks๐Ÿ˜๐Ÿ˜

Caleb Kemp
Caleb Kemp
12,676 Points

I'm very glad you found the comments helpful, it means a lot to me :smile:, I actually had to spend a bit of time looking up the one question you had (it was from a course I hadn't taken yet lol). It is a terrible feeling being stuck, and sometimes you move on because you can't understand the next lesson unless you understand the current one. What I found best is, if there is a particular concept in a video that isn't making sense after watching 2-3 times, I will often do an internet search of it. Hearing it described from multiple perspectives increases the chance you'll hear a version that makes sense to you. I don't check this everyday or anything, but feel free to tag me anytime :smile:

Caleb Kemp
Caleb Kemp
12,676 Points

You probably live in somewhere like Mongolia or something so the first job will end up paying like 10 cents a day, so at the end of the month, it will be like, "Ok, here's your nickel" lol :laughing: I would certainly appreciate if you did that, however, I do have enough for everything I need, so, I think it would probably be best to wait until you get the job and see how your finances look before deciding something like that. I do appreciate you thinking of me though :)

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree
karan Badhwar
Web Development Techdegree Student 13,921 Points

Caleb Kemp, haha no I live in Canada, and belive me the pay scale here is not that bad ๐Ÿ˜‚, but still sir I really appreciate you taking this much time out for me ๐Ÿ™. Thankyou,