title: How to return the value of a function after button click in JQuery?
body: <p>I have a function which returns the output to a promise function when called. </p>
  
  <p>I have a executeScript Method, which dynamically processes Javascript methods and returns a promise as to what those methods return.</p>
  
  <p><strong>Base Skeleton</strong></p>
  
  <pre><code>Ref.executeScript({
                  code: "getData(); function getData(){ var res = 'Hello'; return res;}",
              function(results){
       console.log(results); ////THIS RETURNS "Hello"
     });
  </code></pre>
  
  <p>When the code is directly returning a variable it works. For example, the following returns Hello when executed.</p>
  
  <pre><code>getData();
   function getData(){
     var res = "Hello";
     return res;
    }
  </code></pre>
  
  <p>But if I return it inside the click event, it returns the previous value "Hello"
  and not "Hello2" on button click. I want "Hello2" to be returned. </p>
  
  <pre><code>getData();
  function getData(){
   var res = "Hello";
   jQuery("#myButton").on('click', function(){
     res = "Hello2";
     alert(res); //this gets called
    });
  return res;
  }
  </code></pre>
  
  <p>Updated the Original question to provide more clarity</p>