title: Return variable after setTimeout
body: <p>I'm trying to return a variable only after it has been set within a setTimeout callback. I couldn't think of any other way to do this, but here's my attempt (I know the code seems silly, but it's to work around a Cordova bug). For reasons beyond my understanding, it results in an infinite loop.</p>
  
  <pre><code>function isConnected() {
      var done = false;
      setTimeout(function() {
          done = true;
      }, 500);
  
      while (!done) {}
      return navigator.connection.type!== Connection.NONE;
  }
  </code></pre>
  
  <p>Can anyone explain to me why this is happening, or provide an alternative?</p>
  
  <p><strong>Update (solution):</strong></p>
  
  <pre><code>function isConnected(callback) {
      setTimeout(function() {     
          callback(navigator.connection.type !== Connection.NONE);
      }, 500);
  }
  
  
  isConnected(function(connected) {
      if (!connected)
          alert('Not Connected');
  });
  </code></pre>