title: Wait until a condition is true?
body: <p>I'm using <code>navigator.geolocation.watchPosition</code> in JavaScript, and I want a way to deal with the possibility that the user might submit a form relying on location before <code>watchPosition</code> has found its location.</p>
  
  <p>Ideally the user would see a 'Waiting for location' message periodically until the location was obtained, then the form would submit.</p>
  
  <p>However, I'm not sure how to implement this in JavaScript given its lack of a <code>wait</code> function.</p>
  
  <p>Current code:</p>
  
  <pre><code>var current_latlng = null;
  function gpsSuccess(pos){
      //console.log('gpsSuccess');  
      if (pos.coords) { 
          lat = pos.coords.latitude;
          lng = pos.coords.longitude;
      }
      else {
          lat = pos.latitude;
          lng = pos.longitude;
      }
      current_latlng = new google.maps.LatLng(lat, lng);
  }
  watchId = navigator.geolocation.watchPosition(gpsSuccess,
                    gpsFail, {timeout:5000, maximumAge: 300000});
  $('#route-form').submit(function(event) {
      // User submits form, we need their location...
      while(current_location==null) {
          toastMessage('Waiting for your location...');
          wait(500); // What should I use instead?
      }
      // Continue with location found...
  });
  </code></pre>