Callbacks
function tellMeWhen(callback) {
// you can do stuff here and will be executed every time,
// BEFORE the console.logs below
// but callback() will execute the below functions
callback();
}
tellMeWhen(function(){
console.log("finish");
}) ;
tellMeWhen(function(){
console.log('all over her');
});
// => finish
// => all over her// run the initial function
function greet(callback) {
console.log('Hey!');
// you can include objects in your callback
var data = {
name: 'Piper'
};
// then run a callback to run the callback function
// which will add the callback functions as parameters on the main function
callback(data);
}
// is run after the initial function is run the first time
greet(function(data) {
console.log('Callback function 1 was invoked!');
// get the whole data object
console.log(data); // => { name: 'Piper' }
});
// callback function 2
// run after the initial function is run the second time
greet(function(data) {
console.log('Callback function 2 was invoked!');
// get only the data object name property
console.log(data.name); // => Piper
});