Question

I cannot access the returned reject from this deferred. I think it might be because this function is nested and I have to place a return reject somewhere else. Please advise.Thanks.

I'm expecting the reject value, to be used here:

    $.search(boxes).then(function(results) {
      console.log(results, "place_results length: ", public.place_results.length);
      // public.showPlaces();
    }), function(error) {
      console.log("failed search");
      console.log(error);
    };



$.search = function (boxes) {
      function findNextPlaces(place_results, searchIndex) {
          var dfd = $.Deferred();
          if (searchIndex < boxes.length) {
              // service.nearbySearch({
              service.radarSearch({
                  bounds: boxes[searchIndex],
                  // types: ["food"]
                  keyword: ["coffee"],
                  // rankBy: google.maps.places.RankBy.DISTANCE
              }, function (results, status) {
                  if (status != google.maps.places.PlacesServiceStatus.OK  && status === 'OVER_QUERY_LIMIT') {
                      failed_Queries.push(boxes[searchIndex]);
                      console.log("failed!: boxes:",searchIndex, failed_Queries);
                      // dfd.reject("Request["+searchIndex+"] failed: "+status);
                      return dfd.reject("failed").promise();
                  }
                  if (status != 'OVER_QUERY_LIMIT'){
                    console.log(status);
                    console.log( "bounds["+searchIndex+"] returns "+results.length+" results" );
                    for (var i = 0, result; result = results[i]; i++) {
                        var marker = createMarker(result);
                        public.place_results.push(result.reference); // marker?
                        $(document).trigger('test', result.reference);
                    }                    
                  }
                  dfd.resolve(public.place_results, searchIndex+1);
              });
                return timeDelay(1000).then(function() {
                        return dfd.then(findNextPlaces);                        
                      });
            }
          else {
            return dfd.resolve(public.place_results).promise();
          }
      }
    return findNextPlaces(public.place_results, 0);
  }
Était-ce utile?

La solution

$.search(boxes).then(function(results) {
    …
}), function(error) {
    …
};

That's a syntactical error - you want

$.search(boxes).then(function(results) {
    …
}, function(error) {
    …
});

return dfd.reject("failed").promise();

Is a stylistic issue - you cannot return anything from the callback, and you don't need a .promise() here. It might be better to write

dfd.reject("failed");
return;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top