Another important thing to know is how to properly pass the callback. This is where I have often forgotten the proper syntax.
Callback without arguments
For a callback with no arguments you pass it like this:
myFunction(myCallBack);Note that the second parameter here is simply the function name (but not as a string and without parentheses). Functions in Javascript are 'First class citizens' and so can be passed around like variable references and executed at a later time.
Callback with arguments
"What do you do if you have arguments that you want to pass?", you might ask yourself.
Wrong
The Wrong Way (will not work!)
myFunction(myCallBack(param1, param2));
This will not work because it calls myCallBack(param1, param2) and then passes the return value as the second parameter to myFunction.
Right
The Right Way ( note the use of function(){ preceding myCallBack() )
myFunction(function(){
myCallBack(param1, param2);
});