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

Richard Nash
Richard Nash
24,862 Points

What is a "lifecycle callback"? In fact, what is a "callback"?

I've seen this term thrown around a bunch and i'm still a little hazy on it, but what is a "callback"? Why does it exist and what does it do? Why should I care? etc...

And on top of that I've heard the term "lifecycle callback" which I also don't understand because of my lack of understanding of the first question.

So any light that can be shed on this topic would be most appreciated.

Thank you :-)

1 Answer

Steven Parker
Steven Parker
216,149 Points

A "callback" is essentially a function passed as an argument to another function.

The callback function will be invoked at some later time as a result of some event occurring or process completing. For example:

setTimeout(elapsed, 1000);

In this call to setTimeout, the argument elapsed is the name of a callback function that will be called 1 second (1000 milliseconds) later.

A lifecycle callback would be a similar function that would get called multiple times during the life of an associated entity, unlike the example just given where elapsed would be called only once. It would likely be called to indicate changes in the state of the entity, or to indicate milestones in a concurrent process.