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})
1/*
2A callback function is a function passed into another function
3as an argument, which is then invoked inside the outer function
4to complete some kind of routine or action.
5*/
6function greeting(name) {
7 alert('Hello ' + name);
8}
9
10function processUserInput(callback) {
11 var name = prompt('Please enter your name.');
12 callback(name);
13}
14
15processUserInput(greeting);
16// The above example is a synchronous callback, as it is executed immediately.
1function add(a, b, callback) {
2 if (callback && typeof(callback) === "function") {
3 callback(a + b);
4 }
5}
6
7add(5, 3, function(answer) {
8 console.log(answer);
9});
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