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

Crystal Vesely
14,502 PointsWhy do we have to return the callback in the getJSON()?
Trying to understand why we needed to use the return on callback(data) within the getJSON() function. Could someone walk me through what is happening when we return a function? Where does it go? Does it invoke it within the parent function and then return the value? In previous videos, we did not use a return on the callback. What makes this one different?
2 Answers

Travis Alstrand
Treehouse Project ReviewerHey there Crystal Vesely ! π
This is also explained in the Teacher's Notes below the video, but to put it in other words for hope of clarification:
I haven't watched this video to get caught up on exactly what's going on with the rest of the code, but from the Teacher's Notes it appears using the return
keyword is not necessary in this setting.
In general though, the return callback(data)
statement could serve a specific purpose:
It executes the callback function with the parsed JSON data as an argument. It then returns the result of the callback function to the caller of the parent function.
Here's what's happening step by step:
- The JSON data is parsed and stored in the
data
variable. - The callback function is invoked with
data
as its argument. - The
return
statement ensures that whatever the callback function returns is also returned by the parent function.
This pattern is often used in asynchronous operations to handle the retrieved data. The key differences when using return callback(data)
are:
- It allows the caller to receive the processed result of the callback function.
- It provides a way to chain operations or use the result in a more flexible manner.
- In the first example where return wasn't used, the callback was likely just performing an action without needing to return a value to the caller. The use of
return
here allows for more versatility in how the data is handled after processing.
I hope this makes sense!

Crystal Vesely
14,502 PointsIβm a dunce. I always read the teachers notes, but not in this case. Iβve spent a while researching this topic. Your explanation really helps me to understand whatβs happening under the hood. Thanks so much for this detailed explanation.

Travis Alstrand
Treehouse Project ReviewerNo worries at all π It happens!
And you're very welcome π