Here's a pretty good explanation:
The callback function, also known as a higher-order function, is a function that is passed to another function (allows you to call this other function "otherFunction") as a parameter, and the callback function is called (or executed) inside another. The callback function is essentially a template (established by solving a common problem), and therefore the use of the callback function is also known as the callback template.
callback not a keyword, its just the name of the parameter that is passed to the function, you can call it whatever you want ( callback or cb pretty common).
I will try to explain this with an example of a super simplified custom assembly callback function:
function useAsCallback(string){ console.log("callback is being executed with passed parameter: " + string) } function main(param, callback){ callback(param) } main(123456, useAsCallback)
if you run this, it will print: callback is being executed with passed parameter: 123456
The reverse pattern is commonly used to handle asynchronous JavaScript behavior.
EDIT: a more specific example:
Speaking of the code snippet ... lets say you enter your factory in the controller.
You now have an auth.authenticate method. You must pass two parameters (credentials, callback) .
auth.authenticate({username: Joe, password: 123456}, function(authStatus){ if(authStatus){ console.log("Successfully authenticated") }else{ console.log("Access denied") } });
We simply passed an anonymous function as the callback parameter of our auth.authenticate method.
EDIT: response to "ADDITIONAL INFORMATION":
There seems to be some kind of misunderstanding. You are asking:
A function parameter (authenticated) sends a boolean that defines the functionality inside auth.authenticate ()
The fact is that it is completely opposite: auth.authenticate() passes the value to the (authenticated) 'function, which is an anonymous function. This happens at this point: callback && callback(auth.authenticated); - on .success or callback && callback(false); - on .error