title: How do I return the value of a SignalR function to a parent function when the SignalR function is done?
body: <p>I have a SignalR function that retrieves some data from a hub. </p>
  
  <p>Example:</p>
  
  <pre><code>var getApple = function () {
      hub.server.getFruits("Apple").done(function (result) {
          return result;
      });
  };
  </code></pre>
  
  <p>I want to be able to return the value of <code>getFruits()</code> to <code>getApple()</code> when <code>getFruits()</code> is done;</p>
  
  <pre><code>console.log(getApple());
  </code></pre>
  
  <p>However using this approach, <code>getApple()</code> will always be undefined.</p>
  
  <p>I've tried the following (really ugly) solution </p>
  
  <pre><code>var getApple = function () {
      var result2;
      hub.server.getFruits("Apple").done(function (result) {
          result2 = result;
      });
      return result2;
  };
  </code></pre>
  
  <p>However <code>getApple()</code> was undefined until <code>getFruits()</code> was completed. I was able to workaround this with a <code>setTimeout()</code> but this is obviously a terrible solution.</p>
  
  <p>I've also tried the following but this does not seem the return the actual result of <code>getFruits()</code>.</p>
  
  <pre><code>var getApple = function () {
      return hub.server.getFruits("Apple").done(function (result) {
          return result;
      });
  };
  </code></pre>
  
  <p>From my understanding I'm lacking async javascript/jquery promise(?) knowledge. I've read the documentation and have looked at some other SO questions but was unable to properly place it into context.</p>
  
  <p>I would greatly appreciate a code example of a proper implementation for this.</p>