Praneeth
Praneeth

Reputation: 83

How can I return a value from inside of multiple functions?

Here is the code I'm working with:

(function() {
    var origOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function() {
        this.addEventListener('load', function() 
            console.log(this.responseText); //return this value
        });
        origOpen.apply(this, arguments);
    };
})();

I'm using selenium's execute_script() to execute the above code on to the website. How can I return this.responseText to a variable?

Thank you.

Upvotes: 0

Views: 49

Answers (1)

José M. Carnero
José M. Carnero

Reputation: 263

this.responseText obtain value when XMLHttpRequest responds, asynchronously.

If you need execute code when XMLHttpRequest finish you must pass as a callback, or call directly in the event; inside that call you can redirect the application flux.

Example, directly (I try not to change your original code):

function oCallback(responseText){
    console.log('continues execution');
    console.log(responseText);
}

(function() {
    var origOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function() {
        this.addEventListener('load', function() 
            //console.log(this.responseText); //return this value
            oCallback(this.responseText);
        });
        origOpen.apply(this, arguments);
    };
})();

Upvotes: 1

Related Questions