Is there a way in vanilla Javascript (ES5) to get the caller function and re execute it after an async call is done without the need to pass it as a callback function?
I am building a caching mechanism on a system and for some reason it's not possible for me to use promises, es6's generator function etc (any modern js features which I thought could be of help).
Right now, I'm coding it this way (this is a much simplified version):
var cache = {};
var cacheCallbackQueue = [];
var theCallerFunction = function(someParam){
loadCache("aDataTarget",function(){
theCallerFunction(someParam);
});
// cache will be used here somehow
// if the needed cache haven't been loaded
// the async cacher should re-execute this caller function after the caching is complete
}
var anotherCallerFunction = function(anotherParam){
loadCache("anotherDataTarget",function(){
anotherCallerFunction(anotherParam);
});
}
var loadCache = function(cacheId,callback){
asyncCall(cacheId, function(result){
cache[cacheId] = result;
cacheCallbackQueue.push(callback);
// is there a way to get the caller function automatically
// without the need to pass it as callback function on the parameter
checkCachingStatus();
})
}
// check if caching is completed,
// if the caching is completed,
// it will execute all previously queued callback function
var checkCachingStatus = function(){
var cachingStatus = "complete";
if(!cache["aDataTarget"] || !cache["anotherDataTarget"]){
cachingStatus = "incomplete";
}
if(cachingStatus === "complete"){
for(var key in cacheCallbackQueue){
cacheCallbackQueue[key]();
}
}
};
theCallerFunction("whatever");
anotherCallerFunction(666);
I am not sure if I'm coding it 'the right javascript way', I'm open for another suggestion