callback - overview
[WHAT]
- [] a 'callback' refers to a function that we are passing into another function as an arguement.
[WHY]
- [] the callback function that we pass into the calling function(aka the callee), will be executed once the callee function has completed execution.
[WHERE]
- [] callback - a primer on using callbacks,
[WHEN]
- [] to use a callback - when you want to execute another function immediately after the current function has completed its own execution
[EXAMPLE]
- first function
getDirectionsToLocation(callback){
var directions = "head 4 miles east, turn right at stop sign.Then head 2mile west. location is on left hand side of road"
callback;
}
travelToLocation(){
}
getDirectionsToLocation(travelToLocation);
[HOW-TO]
- ] pass a callback by reference
- ] in the above example we passed the callback arguement(doSomethingMore) by reference,
- ] pass a callback by value
- ] we can also pass callback arguements by value, aka we are passing the function body as the arguement
- ] doSomething(function(){console.log name;})
[REFERENCE]
- ]
( aka higher order function)
- // PASTE the first function into your javascript console and execute it, nothing will happen
- ] function doSomething(){
- var message = "hello";
- }
- // PASTE the following function,
- ] function doSomethingMore(){
- console.log name;
- // PASTE the following function call into the console window and run it, CONGRATS you just created your first callback
- ] doSomething(doSomethingMore);