0
 geocoder.geocode({ 'address': address },

                function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        return 1;
                    }
                    else {
                        //alert("Unfortunately we couldn't understand your request and therefore cannot find your nearest branch.");
                        return false;
                    }
                });

How do I get the return status of function(results, status) for use outside the function? I suppose this is a case where a function is being used as an argument to another function. I need to get the return value of the argument function as the return value of geocoder.geocode()

Mani
  • 379
  • 1
  • 4
  • 22
  • Virtually every answer to this question is wrong - you can't `return` a value from an asynchronous function call - possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Adam Jenkins Jun 17 '15 at 09:32
  • is there a method by which I can get know the status and check outside the function? – Mani Jun 17 '15 at 09:36
  • You have to use a callback. This method is described in the accepted answer to the question I referenced above. – Adam Jenkins Jun 17 '15 at 09:39

6 Answers6

0
var check= function (results, status)

This variable check will be either false or 1 depending on the condition.

AkshayJ
  • 771
  • 6
  • 15
0

This should work. Make the function an object property.

geocoder.geocode({
    'address': address
  },

  getStatus: function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      return 1;
    } else {
      //alert("Unfortunately we couldn't understand your request and therefore cannot find your nearest branch.");
      return false;
    }
  });

console.log(geocoder.getStatus(results, status));
Tony Gustafsson
  • 828
  • 1
  • 7
  • 18
0

Use a callback ensuring that it is visible in the scope of the function body.

var myCallback = function(result) {
  // handle the result here
};

geocoder.geocode({
    'address': address
},

function (results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        myCallback(1);
        return 1;
    } else {
        //alert("Unfortunately we couldn't understand your request and therefore cannot find your nearest branch.");
        myCallback(false);
        return false;
    }
});
alediaferia
  • 2,537
  • 19
  • 22
0
    geocoder.geocode({
            'address': address
        },

        var myFunctReturn :function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                return 1;
            } else {
                //alert("Unfortunately we couldn't understand your request and therefore cannot find your nearest branch.");
                return false;
            }
        });

console.log(geocoder.myFunctReturn );
Dimag Kharab
  • 4,439
  • 1
  • 24
  • 45
0
function (results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        // do actions on success
        return 1;
    } else {
        //alert("Unfortunately we couldn't understand your request and therefore cannot find your nearest branch.");
        // do actions on failuer
        return false;
    }
});

or:

function (results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        funcForResult(1);
        return 1;
    } else {
        //alert("Unfortunately we couldn't understand your request and therefore cannot find your nearest branch.");
        funcForResult(false);
        return false;
    }
});
cybersoft
  • 1,453
  • 13
  • 31
0

Depending on what you are planning there could be a more clever solution, but that should do the trick:

var requestResult;

function callback(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        requestResult = 1;
        return 1;
    } else {
        requestResult = false;
        return false;
    }

}

geocoder.geocode({'address': address}, callback);