1function startWith(message, callback){
2 console.log("Clearly written messages is: " + message);
3
4 //check if the callback variable is a function..
5 if(typeof callback == "function"){
6 callback(); //execute function if function is truly a function.
7 }
8}
9//finally execute function at the end
10startWith("This Messsage", function mySpecialCallback(){
11 console.log("Message from callback function");
12})
1function greeting(name) {
2 alert('Hello ' + name);
3}
4
5function processUserInput(callback) {
6 var name = prompt('Please enter your name.');
7 callback(name);
8}
9
10processUserInput(greeting);
1// A function which accepts another function as an argument
2// (and will automatically invoke that function when it completes - note that there is no explicit call to callbackFunction)
3funct printANumber(int number, funct callbackFunction) {
4 printout("The number you provided is: " + number);
5}
6
7// a function which we will use in a driver function as a callback function
8funct printFinishMessage() {
9 printout("I have finished printing numbers.");
10}
11
12// Driver method
13funct event() {
14 printANumber(6, printFinishMessage);
15}
16
1//Callback functions - are functions that are called AFTER something happened
2
3 const foo = (number, callbackFunction) => {
4 //first
5 console.log(number)
6
7 //second - runs AFTER console.log() happened
8 callbackFunction()
9 }
10
1function createQuote(quote, callback){
2 var myQuote = "Like I always say, " + quote;
3 callback(myQuote); // 2
4}
5
6function logQuote(quote){
7 console.log(quote);
8}
9
10createQuote("eat your vegetables!", logQuote); // 1
11
12// Result in console:
13// Like I always say, eat your vegetables!