sharf
sharf

Reputation: 2143

Chaining functions without callbacks

I have a function deviceUpdate(ID, Token) that I can call to update the list of devices for the user. This function does an ajax call where it displays a loading image until it is finished then displays all the info.

There is one particular time I would like to perform an additional (anonymous) function after the update is finished. How can I do this without embedding the anonymous function inside the deviceUpdate function?

Upvotes: 1

Views: 98

Answers (1)

andrew
andrew

Reputation: 9583

As @Joachim Isaksson suggested, it can be passed in as a parameter.

Example:

function deviceUpdate(ID, Token,callback){
 $.post('url',data,function(response){ //ajax with jQuery post as example
   // your processing of response
    'function' === typeof callback && callback(); // if a function was passed in, run it!
 });
}


deviceUpdate(ID, Token); // for normal use

deviceUpdate(ID, Token,function(){
   // this is the anonymous function to be run in the special case 
});

Upvotes: 6

Related Questions