What is a Callback
Function?
By Roland D. San Nicolas
Functions in Javascript
Edited MDN definition = A “subprogram” that can be called by code externally or
internally to the function. A function is composed of a sequence of statements
called the function body. Values can be passed to a function, and the function will
return a value.
Functions are “First Class” objects which means functions have properties and
methods (a function), and can be passed as arguments to other functions, return
them as values from other functions, and can be assigned to variables.
Functional Programming: improve code composition
A function declaration
function helloWorld() {
console.log(“Hello World”);
}
helloWorld();
A function expression (anonymous)
function(x) {
return //some value
};
(x);
Callback Function Definition
Wikipedia - “A reference to executable code, or a piece of executable code, that is
passed as an argument to other code which is expected to call back (execute) the
argument at some convenient time.”
“Higher Order Function” = the hosting function
“Callback function” = the function within the Higher Order Function.
Javascript statements are performed sequentially (line by line). Callbacks are
useful in performing its code at a specified time as to not block subsequent lines of
code (Synchronous vs Asynchronous).
Asynchronous Programming
Asynchronous programming = writing a program where a function(s) happen at
some “later” point in time as to not “block” the rest of the code (improved end user
experience).
Common with jQuery and I/O programming (send/receive data from file systems
and databases) used in Node.js.
jQuery Callback Function Example
$(“btn1”).click(function(){
console.log(“Button 1 clicked”);
});
Asynchronous Callback Function example
request = prepare_the_request();
send_request_asynchronously(request, function (response){
display (response);
});
//this code block returns immediately allowing subsequent code to continue
(non-blocking). The function parameter will be called when the response is
available.
Resources
Javascriptissexy.com (Concise, good place to start)
Javascript the Good Parts by Douglas Crockford
Eloquent Javascript
You-Dont-Know-JS: “Async & Performance”
Mozilla Developer Network
Youtube: especially the funfunfunction channel - “Higher-order functions” and
“Learning Functional Programming with Javascript” by Anjana Vakil
stackoverflow

Callback Function

  • 1.
    What is aCallback Function? By Roland D. San Nicolas
  • 2.
    Functions in Javascript EditedMDN definition = A “subprogram” that can be called by code externally or internally to the function. A function is composed of a sequence of statements called the function body. Values can be passed to a function, and the function will return a value. Functions are “First Class” objects which means functions have properties and methods (a function), and can be passed as arguments to other functions, return them as values from other functions, and can be assigned to variables. Functional Programming: improve code composition
  • 3.
    A function declaration functionhelloWorld() { console.log(“Hello World”); } helloWorld();
  • 4.
    A function expression(anonymous) function(x) { return //some value }; (x);
  • 5.
    Callback Function Definition Wikipedia- “A reference to executable code, or a piece of executable code, that is passed as an argument to other code which is expected to call back (execute) the argument at some convenient time.” “Higher Order Function” = the hosting function “Callback function” = the function within the Higher Order Function. Javascript statements are performed sequentially (line by line). Callbacks are useful in performing its code at a specified time as to not block subsequent lines of code (Synchronous vs Asynchronous).
  • 6.
    Asynchronous Programming Asynchronous programming= writing a program where a function(s) happen at some “later” point in time as to not “block” the rest of the code (improved end user experience). Common with jQuery and I/O programming (send/receive data from file systems and databases) used in Node.js.
  • 7.
    jQuery Callback FunctionExample $(“btn1”).click(function(){ console.log(“Button 1 clicked”); });
  • 8.
    Asynchronous Callback Functionexample request = prepare_the_request(); send_request_asynchronously(request, function (response){ display (response); }); //this code block returns immediately allowing subsequent code to continue (non-blocking). The function parameter will be called when the response is available.
  • 9.
    Resources Javascriptissexy.com (Concise, goodplace to start) Javascript the Good Parts by Douglas Crockford Eloquent Javascript You-Dont-Know-JS: “Async & Performance” Mozilla Developer Network Youtube: especially the funfunfunction channel - “Higher-order functions” and “Learning Functional Programming with Javascript” by Anjana Vakil stackoverflow