0

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

Kamal
  • 1,922
  • 2
  • 23
  • 37

1 Answers1

0

Is there a way in vanilla Javascript (ES2015) 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?

Not in standard JavaScript, no. There was a nonstandard extension some JavaScript engines added, caller, but it is not standard and is forbidden in strict mode.

I am not sure if I'm coding it 'the right javascript way', I'm open for another suggestion

There are a couple of "right JavaScript ways":

  • Pass the function into loadCache, which you've said you don't want to do (but which you're doing)
  • Have loadCache return an object that provides a means of subcribing to events, and have a "retry" event that you can subscribe to; but subscribing to an event means passing a handler function in, which...you've said you don't want to do :-)

Having said all that, it's unclear why loadCache would need to re-call the function that called it. The standard way of handling this would be to use promises (which you can use in ES5 with a polyfill; the polyfill isn't even that big): loadCache would return a promise and then the code calling it would use it:

var theCallerFunction = function(someParam) {
    loadCache(/*...*/)
        .then(function(data) {
            // Use `data` here
        })
        .catch(function() {
            // Handle failure here
        });
    // Probably no code here, since you want to wait for the cached information
};

or if the caller should handle errors:

var theCallerFunction = function(someParam) {
    return loadCache(/*...*/)
        .then(function(data) {
            // Use `data` here
        });
};
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875