I have developed two custom promises that are quite similar, with the only difference being that they operate on distinct user inputs. Both promises utilize various classes and methods from Google Maps API v-3.
What's puzzling is that when the first promise is invoked, it consistently returns a reject status without ever resolving. Additionally, even though it doesn't resolve, the code within the promise continues to run, displaying multiple console.log()
statements I've added for monitoring purposes.
Here are the two promises:
var checkingDeparture = new Promise((resolve, reject) => {
console.log('1. checkingDeparture called');
var newDeparture = '';
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': $('#startTransfer').val()}, (results, status) => {
console.log('2. checkingDeparture geocoder called');
if(status == google.maps.GeocoderStatus.OK) {
coordPoint = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
if(google.maps.geometry.poly.containsLocation(coordPoint, window.venicePolygon)) {
console.log('3. checkingDeparture containsLocation called');
newDeparture = 'Piazzale Roma, Venezia, VE, Italia';
}
else {
newDeparture = $('#startTransfer').val();
}
}
console.log(newDeparture);
resolve(newDeparture);
});
reject('Sorry there was an error with checkingDeparture promise...');
});
var checkingDestination = new Promise((resolve, reject) => {
console.log('1. checkingDestination called');
var newDestination = '';
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': $('#endTransfer').val()}, (results, status) => {
console.log('2. checkingDestination geocoder called');
if(status == google.maps.GeocoderStatus.OK) {
coordPoint = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
if(google.maps.geometry.poly.containsLocation(coordPoint, window.venicePolygon)) {
console.log('3. checkingDestination containsLocation called');
newDestination = 'Piazzale Roma, Venezia, VE, Italia';
}
else {
newDestination = $('#endTransfer').val();
}
}
console.log(newDestination);
resolve(newDestination);
});
reject('Sorry there was an error with checkingDestination promise...');
});
The following snippet illustrates how these promises are utilized:
var checkedDeparture = checkingDeparture;
var checkedDestination = checkingDestination;
Promise.all([checkedDeparture, checkedDestination]).then(() => {
console.log(checkedDeparture + checkedDestination);
var request = {
origin: checkedDeparture,
destination: checkedDestination,
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC
};
directionsService.route(request, (result, status) => {
console.log('4. checking value of origin: ' + request.origin);
console.log('5. checking value of destination:' + request.destination);
console.log('6. directionsService called');
if(status == google.maps.DirectionsStatus.OK) {
//do things
}
else {
directionsDisplay.setDirections({routes: []});
theMap.setCenter(window.globalCenter);
}
});
}).catch(e => {
console.log(e);
});
Despite extensive testing, I'm unable to pinpoint the source of the issue, particularly since checkingDestination
appears to be resolving correctly. As checkingDeparture
always rejects (but still executes), any subsequent code chained with .then()
won't get executed.