JSFiddle - React, Tailwind, and code Playground
JavaScript
function getLocationByIP(onSuccess, onFailure) {
$.ajax({
url: 'http://freegeoip.net/json/',
dataType: 'json',
type: 'GET',
crossDomain: true,
success: function (data) {
onSuccess(data.latitude, data.longitude);
},
error: function (jqXHR, textStatus, errorThrown) {
onFailure(jqXHR, textStatus, errorThrown);
}
});
}
function getCityDetailsByLocation(latitude, longitude, callback) {
$.when(getClosestCityNameByLocation(latitude, longitude))
.then(function(data) {
return getCityDetailsByName(data);
})
.then(function(data) {
alert(data);
});
}
function getClosestCityNameByLocation(latitude, longitude) {
var deferred = $.Deferred();
deferred.resolve('Atlanta');
return deferred.promise();
}
function getCityDetailsByName(searchTerm) {
var deferred = $.Deferred();
deferred.resolve('Georgia');
return deferred.promise();
}
getLocationByIP(getCityDetailsByLocation);