title: JavaScript Asynch Callback - How to await the endresult
body: <p>I have the following situation (see also jsFiddle -> <a href="http://jsfiddle.net/sMuWK/" rel="nofollow">http://jsfiddle.net/sMuWK/</a>):</p>
  
  <pre><code>function CallBackStringHandler() {
      this.callback = function(){return null};
  };
  
  CallBackStringHandler.prototype.doTheMagic = function(callback) {
      var result = callback.call(this);
          if(result == null)
              alert("Nothing to handle yet...");
          else
              alert("End the result is: \n\n" + result);
  };
  
  function Action(){
      var result = null;
      var max = 10;
      var index = 0;
      var processor = setInterval(function(){
          if(index &lt;= max){ //Processing step
              if(result == null)
                  result = "" + index;
              else
                  result += index;
              index++;
          } else { //Done
              clearInterval(processor);
              alert(result);
          }
      },10);
      return result;
  };
  
  function Run(){    
      var handler = new CallBackStringHandler();
      handler.doTheMagic(Action);
  };
  
  Run();
  </code></pre>
  
  <p>A script (a jQuery plugin) allows you to specify a callback that has to return a string.
  This string will be handled by this script.</p>
  
  <p>So far so good.</p>
  
  <p>For the sake of performance and keeping my page responsive, I want to build this string in a multi-threaded way. Since this is not a web standard yet, I simulate this with the help of setInterval.</p>
  
  <p>Now I know that the essence of doing things this way is not waiting for the results.</p>
  
  <p>But I can't think of a way of keeping things responsive and fast and return the full result to the handler.</p>
  
  <p>So the end result (in this example) should show: 012345678910.</p>
  
  <p>Any help/clues would be appreciated.</p>
  
  <p>Cheers, another nerd.</p>