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.
This question is related to
javascript
function
callback
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
the proper implementation would be:
if( callback ) callback();
this makes the callback parameter optional..
Here is a basic example that explains the callback()
function in JavaScript:
var x = 0;_x000D_
_x000D_
function testCallBack(param1, param2, callback) {_x000D_
alert('param1= ' + param1 + ', param2= ' + param2 + ' X=' + x);_x000D_
if (callback && typeof(callback) === "function") {_x000D_
x += 1;_x000D_
alert("Calla Back x= " + x);_x000D_
x += 1;_x000D_
callback();_x000D_
}_x000D_
}_x000D_
_x000D_
testCallBack('ham', 'cheese', function() {_x000D_
alert("Function X= " + x);_x000D_
});
_x000D_
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.)
the proper implementation would be:
if( callback ) callback();
this makes the callback parameter optional..
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.)
You can use:
if (callback && typeof(callback) === "function") {
callback();
}
The below example is little more comprehensive:
function mySandwich(param1, param2, callback) {_x000D_
alert('Started eating my sandwich.\n\nIt has: ' + param1 + ', ' + param2);_x000D_
var sandwich = {_x000D_
toppings: [param1, param2]_x000D_
},_x000D_
madeCorrectly = (typeof(param1) === "string" && typeof(param2) === "string") ? true : false;_x000D_
if (callback && typeof(callback) === "function") {_x000D_
callback.apply(sandwich, [madeCorrectly]);_x000D_
}_x000D_
}_x000D_
_x000D_
mySandwich('ham', 'cheese', function(correct) {_x000D_
if (correct) {_x000D_
alert("Finished eating my " + this.toppings[0] + " and " + this.toppings[1] + " sandwich.");_x000D_
} else {_x000D_
alert("Gross! Why would I eat a " + this.toppings[0] + " and " + this.toppings[1] + " sandwich?");_x000D_
}_x000D_
});
_x000D_
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
You can use:
if (callback && typeof(callback) === "function") {
callback();
}
The below example is little more comprehensive:
function mySandwich(param1, param2, callback) {_x000D_
alert('Started eating my sandwich.\n\nIt has: ' + param1 + ', ' + param2);_x000D_
var sandwich = {_x000D_
toppings: [param1, param2]_x000D_
},_x000D_
madeCorrectly = (typeof(param1) === "string" && typeof(param2) === "string") ? true : false;_x000D_
if (callback && typeof(callback) === "function") {_x000D_
callback.apply(sandwich, [madeCorrectly]);_x000D_
}_x000D_
}_x000D_
_x000D_
mySandwich('ham', 'cheese', function(correct) {_x000D_
if (correct) {_x000D_
alert("Finished eating my " + this.toppings[0] + " and " + this.toppings[1] + " sandwich.");_x000D_
} else {_x000D_
alert("Gross! Why would I eat a " + this.toppings[0] + " and " + this.toppings[1] + " sandwich?");_x000D_
}_x000D_
});
_x000D_
function checkCallback(cb) {_x000D_
if (cb || cb != '') {_x000D_
if (typeof window[cb] === 'undefined') alert('Callback function not found.');_x000D_
else window[cb].call(this, Arg1, Arg2);_x000D_
}_x000D_
}
_x000D_
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
function checkCallback(cb) {_x000D_
if (cb || cb != '') {_x000D_
if (typeof window[cb] === 'undefined') alert('Callback function not found.');_x000D_
else window[cb].call(this, Arg1, Arg2);_x000D_
}_x000D_
}
_x000D_
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.)
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
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!
Here is a basic example that explains the callback()
function in JavaScript:
var x = 0;_x000D_
_x000D_
function testCallBack(param1, param2, callback) {_x000D_
alert('param1= ' + param1 + ', param2= ' + param2 + ' X=' + x);_x000D_
if (callback && typeof(callback) === "function") {_x000D_
x += 1;_x000D_
alert("Calla Back x= " + x);_x000D_
x += 1;_x000D_
callback();_x000D_
}_x000D_
}_x000D_
_x000D_
testCallBack('ham', 'cheese', function() {_x000D_
alert("Function X= " + x);_x000D_
});
_x000D_
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!
Source: Stackoverflow.com