+ 2
A callback function is used when we hand off execution to another thread over which we have no control. The idea is that one of my parameters is a delegate or a reference pointer to the method i want to execute whenever the other system is done with my code.
Here is an example for javascript.
function FetchDataFromServer (callback){
$.ajax ({
method:'post',
url:'http;//www.test.com/data',
success:function(data){
callback (data);
}})
}
callback=function(data){
alert (data);
};
FetchDataFromServer (callback);
so only once the data is received will i run the callback that processes the data. This is important in javascript because you only have one thread is if we had to wait for the data all scripts will freeze.
your code -> ask system to do task
system -> tells your code task is done using callback
the system runs calls the callback function running that function.
I hope this makes sense to you and i don't sound condescending.