Question

I am overriding a javascript function like this :

(function() {
    origFunc = aFunction;
    aFunction = function() {
        doRequest();                
        //return origFunc.apply(this);
    };
})();

I know that I need to end the function with "return origFunc.apply(this)" in order to execute the original function. However, as I'm executing a request, I have to wait until the request is done. That's why I wrote this function :

doRequest: function()
{
    try
    {
        if(window.XMLHttpRequest)
            httpRequest = new XMLHttpRequest();
        else if(window.ActiveXObject)
            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");

        var url = anUrl, self = this;

        httpRequest.onreadystatechange = function(data)
        {
            try
            {
                if(httpRequest.readyState == 4)
                {
                    if(httpRequest.status == 200)
                       return origFunc.apply(self);
                    else if(httpRequest.status != 0 )
                       alert("Error while executing the request : "+httpRequest.status+"\r\nUrl : "+url);
                }
            }
            catch(e)
            {                    
            }
        };

        httpRequest.open("GET", url);
        httpRequest.send();
    }
    catch(err)
    {
        alert("Error : "+err);
    }        
}

As you can guess, the problem is that I can't do the things like that. Do you know how I could do ?

Was it helpful?

Solution

Here is an example for how to deal with wrapping async functions

// This simply calls the callback with some data after a second
// Could be an AJAX call for example
var doSomethingAsync = function (callback) {
  setTimeout(function () {
    callback({ some: 'data' });
  }, 1000);
};

var fnThatMakesAsyncCall = function () {
  // From the outside there is no way to change this callback
  // But what if we need to intercept the async function to change the data given to it, or monitor it?
  // Then we'd have to wrap the async function to wrap the callback.
  var callback = function (data) {
    console.log('Original', data); 
  };

  doSomethingAsync(callback);
};

// Function to wrap another function and return the wrapper
var wrapFn = function (fn) {
  // Create the wrapped function.
  // Notice how it has the same signature with `callback` as the first argument
  var wrapped = function (callback) {
    // Here we get the original callback passed in
    // We will instead wrap that too and call the original function with our new callback
    var newCb = function (data) {
      // This will run once the async call is complete
      // We will as an example mutate the data in the return data of the callback
      data.some = 'Wrapped it';
      // Call the original callback with the changed data
      callback.call(this, data);
    };
    // Run the function we wrap with the new callback we supply
    fn.call(this, newCb);
  };
  // Return wrapped function
  return wrapped;
};

// Will log Original {some: "data"} 
fnThatMakesAsyncCall();
doSomethingAsync = wrapFn(doSomethingAsync);
// Will log Original {some: "Wrapped it"} 
fnThatMakesAsyncCall();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top