I have a function that includes a call to another function which returns a set of coordinates. However, when this function is called, it doesn't wait for the return of the coordinates, instead it continues, and when I try to print the returned value(an array), it is always undefined. How can force my code to wait for the return of the coordinates?
Where it's being called:
$('#arena-form').on('submit', function(e) {
e.preventDefault();
fullAddress = "80 Devon Street, Brantford, Ontario, Canada";
var locationInfo = [];
locationInfo = geocodeQuery(fullAddress); // calling the function that will return the coordinates
console.log(locationInfo);
});
Function that Returns the Coordinates
function geocodeQuery(address) {
if (!searchManager) {
Microsoft.Maps.loadModule('Microsoft.Maps.Search', function () {
searchManager = new Microsoft.Maps.Search.SearchManager(map);
geocodeQuery(address);
});
} else {
var searchRequest = {
where: address,
callback: function (r) {
if (r && r.results && r.results.length > 0) {
var locationInfo = [r.results[0].location.latitude, r.results[0].location.longitude];
return(locationInfo);
}
},
errorCallback: function (e) {
showModalAlert("danger", "location not found");
}
};
//Make the geocode request.
searchManager.geocode(searchRequest);
}
}