Here is my (simple) problem :
I have a Javascript function which calls an external API in order to get some result asynchronously. I need to wait these results because I want to do some tests on them to determine if they are valid or not, but Deferred
are very complex to me and I can't success.
Here is what I've done :
$("#step-content").steps({
//some parameters
onStepChanging: function(event, currentIndex, newIndex) {
verifyAddress().done(function(test) {
if($("#hdnLatitude").val() == "" || $("#hdnLongitude").val() == "")
test = false;
else
test = true;
console.log(test); // test is true or false, that's good
return test;
});
console.log(test); // test is always empty here
//Here, I just need to do return true or return false to block step-changing if there is an error.
return test;
}
});
Basically, here is my verifyAddress
function :
function verifyAddress() {
var r = $.Deferred();
var geocoder = new google.maps.Geocoder();
if (geocoder) {
var adressToGeocode = /* Get the address to geocode */
geocoder.geocode({ 'address': adressToGeocode }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
//Save the lat/lng returned in #hdnLatitude and #hdnLongitude
}
r.resolve();
});
}
//Not sure where to place the return r ; try elsewhere but no success
return r;
}
So what I need is wait for the end of verifyAdress()
and get the #hdnLatitude and #hdnLongitude filled, and return true of false in onStepChanging
event to determine if we can go to next step (address is OK) and not (address is wrong)
I'm using this SO question to better understand Deferred
, but I can't success.
Is anyone able to help me ?
Thanks a lot