Reputation:
I understand passing in a function to another function as a callback and having it execute, but I'm not understanding the best implementation to do that. I'm looking for a very basic example, like this:
var myCallBackExample = {
myFirstFunction : function( param1, param2, callback ) {
// Do something with param1 and param2.
if ( arguments.length == 3 ) {
// Execute callback function.
// What is the "best" way to do this?
}
},
mySecondFunction : function() {
myFirstFunction( false, true, function() {
// When this anonymous function is called, execute it.
});
}
};
In myFirstFunction, if I do return new callback(), then it works and executes the anonymous function, but that doesn't seem like the correct approach to me.
Upvotes: 164
Views: 209230
Reputation: 24948
You can use:
if (callback && typeof(callback) === "function") {
callback();
}
The below example is little more comprehensive:
function mySandwich(param1, param2, callback) {
alert('Started eating my sandwich.\n\nIt has: ' + param1 + ', ' + param2);
var sandwich = {
toppings: [param1, param2]
},
madeCorrectly = (typeof(param1) === "string" && typeof(param2) === "string") ? true : false;
if (callback && typeof(callback) === "function") {
callback.apply(sandwich, [madeCorrectly]);
}
}
mySandwich('ham', 'cheese', function(correct) {
if (correct) {
alert("Finished eating my " + this.toppings[0] + " and " + this.toppings[1] + " sandwich.");
} else {
alert("Gross! Why would I eat a " + this.toppings[0] + " and " + this.toppings[1] + " sandwich?");
}
});
Upvotes: 2
Reputation: 6411
function checkCallback(cb) {
if (cb || cb != '') {
if (typeof window[cb] === 'undefined') alert('Callback function not found.');
else window[cb].call(this, Arg1, Arg2);
}
}
Upvotes: 1
Reputation: 6280
Here is a basic example that explains the callback()
function in JavaScript:
var x = 0;
function testCallBack(param1, param2, callback) {
alert('param1= ' + param1 + ', param2= ' + param2 + ' X=' + x);
if (callback && typeof(callback) === "function") {
x += 1;
alert("Calla Back x= " + x);
x += 1;
callback();
}
}
testCallBack('ham', 'cheese', function() {
alert("Function X= " + x);
});
Upvotes: 1
Reputation: 77171
You can just say
callback();
Alternately you can use the call
method if you want to adjust the value of this
within the callback.
callback.call( newValueForThis);
Inside the function this
would be whatever newValueForThis
is.
Upvotes: 133
Reputation: 2664
You should check if the callback exists, and is an executable function:
if (callback && typeof(callback) === "function") {
// execute the callback, passing parameters as necessary
callback();
}
A lot of libraries (jQuery, dojo, etc.) use a similar pattern for their asynchronous functions, as well as node.js for all async functions (nodejs usually passes error
and data
to the callback). Looking into their source code would help!
Upvotes: 91
Reputation: 61
the proper implementation would be:
if( callback ) callback();
this makes the callback parameter optional..
Upvotes: 6
Reputation: 179119
There are 3 main possibilities to execute a function:
var callback = function(x, y) {
// "this" may be different depending how you call the function
alert(this);
};
The method you choose depends whether:
Docs for Function.call, Function.apply
Upvotes: 34
Reputation: 75814
Callbacks are about signals and "new" is about creating object instances.
In this case it would be even more appropriate to execute just "callback();" than "return new callback()" because you aren't doing anything with a return value anyway.
(And the arguments.length==3 test is really clunky, fwiw, better to check that callback param exists and is a function.)
Upvotes: 6