title: Code doesn't wait for Callback Function Completion
body: <p>I am new to Javascript coming from Java and want to write a Function who acts like a Java Method. 
  The Problem is that the calling Code doesnt wait for the called function to finish and just continues what occurrs errors. i hope you understand what I'm talking about. Maybe you can help me</p>
  
  <pre><code>if (Titanium.Facebook.loggedIn) {
          // User is not logged in with his ACS Account but with its Facebook Account
          // now we can check if he has an ACS Account with the same eMail Address
          Titanium.API.info('User seems to be logged in to Facebook');
  
          gFacebookAccount = gFacebookController.getLoggedInFacebookAccount();
          Titanium.API.info('Logged In Facebook Account is : '+ gFacebookAccount); }
  </code></pre>
  
  <p>The called getLoggedInFacebookAccount looks like this. i tried to delay the method by a timeout loop what doesnt work.</p>
  
  <pre><code>exports.getLoggedInFacebookAccount = function()
  {
  Titanium.API.info('Started getLoggedInFacebookAccount');
  var lCallBackFinished = false;
  var lActualUser = null;
  Titanium.Facebook.requestWithGraphPath('me', {}, 'GET', handleGraphRequestCallback);
  
  waitForQueryCallback();
  
  
  function waitForQueryCallback() {
      if (!lCallBackFinished) {
          Titanium.API.info('Wait for Query Callback Loop');
          setTimeout(waitForQueryCallback, 100);
          //
      }
      else
      {
          return lActualUser;
      }
  }
  
  
  function handleGraphRequestCallback(pGraphCallback) {
  
  // damn callbacks - now here the user is logged in
  // and the Graph Request is (sucessfully or not) finished
  
  Titanium.API.info('Start handleGraphRequest-Callback');
  if (pGraphCallback.success) {
      Titanium.API.info('Graph Request Callback Sucessful');
      lActualUser = pGraphCallback.result;
  
  } else {
      Titanium.API.info('Graph Request Callback Not Sucessful');
  
  }
  
  lCallBackFinished = true;
  
  }
  
  }
  </code></pre>
  
  <p>The Console Output is :</p>
  
  <pre><code>[INFO][TiAPI   (  301)] User seems to be logged in to Facebook
  [INFO][TiAPI   (  301)] Started getLoggedInFacebookAccount
  [INFO][TiAPI   (  301)] Wait for Query Callback Loop
  [INFO][TiAPI   (  301)] Logged In Facebook Account is : undefined
  [INFO][TiAPI   (  301)] Function called
  [ERROR][TiJSError(  301)] (main) [214,2413] ----- Titanium Javascript Runtime Error -----
  [ERROR][TiJSError(  301)] (main) [0,2413] - In undefined:1,1
  
  [ERROR][TiJSError(  301)] (main) [0,2413] - Message: Uncaught SyntaxError: Unexpected token u
  [ERROR][TiJSError(  301)] (main) [1,2414] - Source: undefined
  [ERROR][V8Exception(  301)] Exception occurred at undefined:1: Uncaught SyntaxError: Unexpected token u
  [INFO][TiAPI   (  301)] Wait for Query Callback Loop
  [INFO][TiRootActivity(  301)] (main) [0,0] checkpoint, on root activity resume. activity = de.dosofun.knowitall.Knowitall2Activity@44ede8f8
  [INFO][TiAPI   (  301)] Wait for Query Callback Loop
  [INFO][ARMAssembler(   60)] generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x3666d0:0x36678c] in 4662744 ns
  [INFO][TiAPI   (  301)] Wait for Query Callback Loop
  [INFO][TiAPI   (  301)] Wait for Query Callback Loop
  [INFO][ActivityManager(   60)] Displayed activity de.dosofun.knowitall/.Knowitall2Activity: 5509 ms (total 5509 ms)
  [INFO][TiAPI   (  301)] Wait for Query Callback Loop
  [INFO][TiAPI   (  301)] Wait for Query Callback Loop
  [INFO][TiAPI   (  301)] Wait for Query Callback Loop
  [INFO][TiAPI   (  301)] Wait for Query Callback Loop
  [INFO][TiAPI   (  301)] Start handleGraphRequest-Callback
  [INFO][TiAPI   (  301)] Graph Request Callback Sucessful
  </code></pre>
  
  <p>cause of line: [INFO][TiAPI   (  301)] Logged In Facebook Account is : undefined
  you can see that the code doesnt wait until the called method is finished. </p>
  
  <p>What is necessary so my code works as wished and if my code is very bad, what is a better way (which is not to confusing to read)</p>
  
  <p>Thanks for your help</p>