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!

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 Object-Oriented JavaScript: Challenge Adding the Game Logic Build the updateGameState() Method

Callback Function Definition

I am a little unclear about the following definition of a callback function that was provided to us in this instruction step.

"A callback function is just a function that's not executed until another function is complete. The callback function is passed as an argument to the first function."

The part I am unsure about is the first sentence. My understanding of a callback function is that it is simply a function passed into another function as an argument to be used in some way and at some point by the function it is passed into, the outer function. I didn't know that the outer function had to be complete first before the callback function gets executed. Is this correct? I'm not sure how that makes sense. Isn't the outer function using the callback function to complete whatever it's supposed to do? Therefore, it can't be complete until the callback function is utilized. Now, if they mean to say that the callback function is just the last thing that gets executed within the outer function, isn't that also false. Doesn't the outer function often use the callback function in the middle of its processes to compute x for example with following lines of code in that outer function then utilizing x?

In this project, isn’t the reason why the callback function runs last because of the .animate jQuery method/syntax it is passed into and not because callback functions only execute after its outer function is β€œcomplete”?

1 Answer

Steven Parker
Steven Parker
228,001 Points

You're right, a callback function could be called at any time in the other function, not necessarily last. But it's very common to use it as a way of returning a result when the "function is complete".

This simplified definition was likely provided here because it relates to the specific examples being shown.