Getting a better understanding of callback functions in JavaScript
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.
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!