我有一个函数,该函数包括对另一个函数的调用,该函数返回一组坐标。但是,当调用此函数时,它不等待坐标的返回,而是继续,并且当我尝试打印返回的值(数组)时,始终是未定义的。如何强制我的代码等待坐标返回?
被称为:
$('#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 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);
}
}
您正在处理的称为异步调用,这仅意味着您必须等到返回Promise