3 ms·
I'm not sure this makes a strong case. Especially since in this case the callback could be "simplified" by having it such that the callback example looks like:
by taeric 13y ago
I'm not sure this makes a strong case. Especially since in this case the callback could be "simplified" by having it such that the callback example looks like:
void loadData = function(){
showLoadingScreen();
$.ajax("http://someurl.com", {
complete: hideLoadingScreen(function(data){
//do something with the data
})
});
};
Where hideLoadingScreen is simply:
function hideLoadingScreen(f) {
return function(e) {
f(e);
doHide();
};
}
You could probably take this further. Though, I'm not sure how valuable that is.
- brlewis 13y agoIn your code, loadData() function knows that the data is being fetched asynchronously. In the posted code, loadData() doesn't have to know whether getData() works synchronously or asynchronously.
- taeric 13y agoMeh. With higher order functions, this could be achieved as well. I was focusing on the "hide the loading screen" portion of the example. And, honestly, it is not obvious to me when the loading screen gets hidden. In the callback one, that is much more obvious. (Granted, this is as much from familiarity.)
- brlewis 13y agoHiding synchronous vs. asynchronous with higher-order functions means doing it with callbacks. It's generally hard to take a function that was originally written to run synchronously and change it to run asynchronously without changing the way it's called. I don't see this as world-changing either, but I understand why people like it.