Page 1 of 1

signature with a callback

Posted: Mon Feb 04, 2013 1:09 am
by Live24x7
Hi
I am trying to figure out the use of callback functions in asynchronous requests in AJAX.

I come across terms like:
"signature with a callback" or "delegate"

Would really appreciate if some one could explain what signature means in this context.

Code: Select all


function mySandwich(param1, param2, callback) {  
    alert('Started eating my sandwich.\n\nIt has: ' + param1 + ', ' + param2);  
  
    $('#sandwich').animate({  
                opacity: 0  
    }, 5000, function() {  
        // Animation complete.  
    });  
  
    if (callback && typeof(callback) === "function") {  
        callback(param1, param2);  
    }  
}  

mySandwich('ham', 'cheese', function(callbackParam1, callbackParam2) {  
    alert('Finished eating my ' + callbackParam1 + ' & ' + callbackParam2 + ' sandwich.');  
}); 

thanks a lot

Re: signature with a callback

Posted: Mon Feb 04, 2013 5:33 am
by requinix
In any language a signature of a function is the combination of its return value and the parameter types and count. The signature may only technically include, for example, the parameter count if the language doesn't support return value types and parameter types (like Javascript), but as a developer you're still responsible for accepting the right types of arguments and returning the right type of value.

So the signature in your example is

Code: Select all

function(callbackParam1, callbackParam2) {
and used by

Code: Select all

callback(param1, param2);
Specifically, the function (named "callback") should take two (or more) arguments; its return value isn't used so it doesn't matter.