SlideShare a Scribd company logo
2
Most read
3
Most read
9
Most read
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

More Related Content

PDF
Php introduction
PPTX
Event In JavaScript
PDF
JavaScript - Chapter 4 - Types and Statements
PPT
Introduction to Javascript
PDF
JavaScript Programming
PPT
Oops concepts in php
PPTX
Lab #2: Introduction to Javascript
PPTX
Java script
Php introduction
Event In JavaScript
JavaScript - Chapter 4 - Types and Statements
Introduction to Javascript
JavaScript Programming
Oops concepts in php
Lab #2: Introduction to Javascript
Java script

What's hot (20)

PPTX
Javascript functions
PPTX
Javascript 101
PDF
The New JavaScript: ES6
PDF
TypeScript: coding JavaScript without the pain
PPTX
Php.ppt
PPT
jQuery Ajax
PDF
jQuery for beginners
PPTX
Html5 and-css3-overview
PDF
ES6 presentation
PDF
JavaScript Promises
PPSX
Php and MySQL
PDF
Chap 4 PHP.pdf
PPTX
Ajax ppt - 32 slides
PPTX
Css selectors
PDF
Angular Observables & RxJS Introduction
PPT
Javascript
PPT
Asynchronous JavaScript & XML (AJAX)
PPSX
Javascript variables and datatypes
Javascript functions
Javascript 101
The New JavaScript: ES6
TypeScript: coding JavaScript without the pain
Php.ppt
jQuery Ajax
jQuery for beginners
Html5 and-css3-overview
ES6 presentation
JavaScript Promises
Php and MySQL
Chap 4 PHP.pdf
Ajax ppt - 32 slides
Css selectors
Angular Observables & RxJS Introduction
Javascript
Asynchronous JavaScript & XML (AJAX)
Javascript variables and datatypes
Ad

Viewers also liked (11)

PPT
Wow! closure in_javascript
PPTX
JavaScript closures
PPT
Ambient Media Worldwide Ltd Jd Lr
PPTX
javascript function & closure
PDF
JavaScript closures
PDF
Asynchronous JavaScript and Promises
PPTX
Closure
PDF
Callbacks, promises, generators - asynchronous javascript
PPTX
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
PPTX
JavaScript tips - Unnest callbacks and method declarations
PDF
The Top Skills That Can Get You Hired in 2017
Wow! closure in_javascript
JavaScript closures
Ambient Media Worldwide Ltd Jd Lr
javascript function & closure
JavaScript closures
Asynchronous JavaScript and Promises
Closure
Callbacks, promises, generators - asynchronous javascript
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
JavaScript tips - Unnest callbacks and method declarations
The Top Skills That Can Get You Hired in 2017
Ad

Similar to Callback Function (20)

PPTX
Avoiding Callback Hell with Async.js
PDF
JavaScript Interview Questions 2023
PDF
Introduction to Object Oriented Javascript
PDF
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
ODP
Introduction of Object Oriented JavaScript
PDF
[2015/2016] JavaScript
PPTX
An introduction to Object Oriented JavaScript
PDF
JavaScript for real men
PDF
Asynchronous programming done right - Node.js
PDF
JavaScript
PDF
Asynchronous JavaScript Programming
PPTX
JS Event Loop
PDF
All you need to know about JavaScript Functions
ODP
Object Oriented Javascript
PDF
Introduction to Node JS2.pdf
PDF
Javascript internals
PDF
Avoiding callback hell with promises
PPTX
Modular javascript
PPTX
Async discussion 9_29_15
Avoiding Callback Hell with Async.js
JavaScript Interview Questions 2023
Introduction to Object Oriented Javascript
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
Introduction of Object Oriented JavaScript
[2015/2016] JavaScript
An introduction to Object Oriented JavaScript
JavaScript for real men
Asynchronous programming done right - Node.js
JavaScript
Asynchronous JavaScript Programming
JS Event Loop
All you need to know about JavaScript Functions
Object Oriented Javascript
Introduction to Node JS2.pdf
Javascript internals
Avoiding callback hell with promises
Modular javascript
Async discussion 9_29_15

Callback Function

  • 1. What is a Callback Function? By Roland D. San Nicolas
  • 2. 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
  • 3. A function declaration function helloWorld() { 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 Function Example $(“btn1”).click(function(){ console.log(“Button 1 clicked”); });
  • 8. 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.
  • 9. 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