Is there a way to catch exceptions in JavaScript callbacks? Is it even possible?
Uncaught Error: Invalid value for property
If you can use Promises and async/await, it can be solved as shown in sample code below:
async function geocode(zipcode) {
return new Promise((resolve, reject) => {
const g = new google.maps.Geocoder().geocode({ 'address': zipcode }, function(geoResult, geoStatus) {
if (geoStatus != google.maps.GeocoderStatus.OK) {
reject(new Error("Callback Exception caught"));
} else {
resolve(g);
};
});
});
}
try {
// ...
// g will be an instance of new google.maps.Geocoder().geocode..
// you can resolve with desired variables
const g = await geocode(zipcode);
// ...
} catch( e ) {
console.log(e);
}