var $ = require('jquery'),
    _ = require('underscore');

var location = function(hasGeolocation) {
  var lat, lon;

  if ( hasGeolocation ) {
    navigator.geolocation.getCurrentPosition(function(position) {
      lat = position.coords.latitude;
      lon = position.coords.longitude;
    }, function(error) {
      console.log(error);
    });
  } else {
    // for local development
    lat = 37.782355;
    lon = -122.409825;
  }

  return {lat: lat, lon: lon};
};

var queryFunction = function(runWhere) {
  if ( runWhere === 'cloud' ) {
    return _.partial(Parse.Cloud.run, "browse");
  } else {
    return require('cloud/lib/browse');
  }
};

var submit = function(params) {
  // choose where to run the query
  var query = queryFunction(params.runwhere);

  // add location if proximity sorting
  if ( params.sort === 'near' ) {
    // false until we start testing geolocation
    $.extend(params, location(false));
  }

  query(params, {
    success: function(result) {
      dumpToDOM(result);
    }, error: function(err) {
      console.error(err);
    }
  });
};

module.exports = { submit: submit };

@@ -676,16 +676,87 @@
 rams) %7B%0A
+  // to keep track of when it finishes%0A  var deferred = $.Deferred();%0A%0A
   // cho
@@ -1049,16 +1049,51 @@
  %7B%0A     
+ deferred.resolve(result);%0A      //
  dumpToD
@@ -1110,16 +1110,21 @@
 ;%0A    %7D,
+%0A%0A   
  error: 
@@ -1149,21 +1149,23 @@
     
-console.error
+deferred.reject
 (err
@@ -1178,16 +1178,46 @@
  %7D%0A  %7D);
+%0A%0A  return deferred.promise();
 %0A%7D;%0A%0Amod

make lib/query .submit return a promise