I can't get my head around callbacks. Here is a simple example to show what I'm confused about. The callback printData needs to be called after getData has returned.
function data(cb) {
var myData = getData();
cb(myData);
}
function printData(data) {
console.log("printing " + data)
}
function getData () {
console.log("getting data");
setTimeout(function(){
console.log("finishing data");
return "this is the data"
}), 3000;
}
data(printData);
//OUTPUT
//getting data
//printing undefined
//finishing data