Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Write a Basic Fetch Request

Converting Arrow function into regular function

Hello there,

Simple question here. I am trying to follow Guil's video and I am trying to re-write the syntax of his functions. I believe my syntax is correct, however i am not getting the same results.

He's writing:

fetch('https://dog.ceo/api/breeds/image/random')
.then(response => response.json())
.then(data => console.log(data))

The console logs the followoing message, which is what we are expecting:

{message: "https://images.dog.ceo/breeds/bulldog-boston/n02096585_2671.jpg", status: "success"} app.js:9 Fetch finished loading: GET "https://dog.ceo/api/breeds/image/random".

Then, I rewrite the syntax. This is my version of the functions, slightly longer:

fetch('https://dog.ceo/api/breeds/image/random')
.then((response) => {
    response.json();
  })

.then((data) => {
    console.log(data);
  })

I am not getting all this information back in the console. I am getting a plain "undefined" message along with "Fetch finished loading: GET "https://dog.ceo/api/breeds/image/random". "

Why is that?

2 Answers

Steven Parker
Steven Parker
231,275 Points

The short form of an arrow function body (a single expression without braces) also implies a return statement.

To make your longer form function as a proper replacement, you still need to add the "return"s (well, at least the first one — I don't think "console.log" returns anything).

Steven Parker Thank you!