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

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
231,124 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.