Question

i am looking for a workaround for getting the google search method to return the results of the callback rather than undefined.

$.find_places_nearby = function(latitude, longitude, successCallback) {
  var latlng, service;
  latlng = new google.maps.LatLng(latitude, longitude);
  service = new google.maps.places.PlacesService(document.getElementById('map'));
  return service.search({
    location: latlng,
    radius: '50',
  }, function(results, status) {
    return successCallback(results, status);
  });
};

for example... i want

$.find_places_nearby(2.4, -100.5, function(results, status) {
  return "callback results";
});

to return callback results instead of undefined

Was it helpful?

Solution

That's not possible. The callback function is explicitly executed at some undefined later point in time, asynchronously. The call to $.find_places_nearby() will explicitly not return anything, rather you are passing code that should be executed with the result of that call as callback. It does not make sense to return something from that callback.

OTHER TIPS

You could try using promise objects. The promise pattern lets you do asynchronous stuff but still maintain a more linear flow.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top