Задача стоит в том, чтобы если LocalStorage пуст, то делаем запрос к API
$.getJSON('http://ip-api.com/json', function(data){
queryParams['lat'] = data.lat;
queryParams['long'] = data.lon;
callApi('towns', queryParams, '').then((res) => {
this.currentCity = res.data.data[0];
})
}.bind(this));
иначе, используем данные из LocalStorage.
Пробовала делать с помощью метода
getTownFromLocalStorage(town) {
var list = JSON.parse(localStorage.getItem('town'));
if (list == null) {
list = []
$.getJSON('http://ip-api.com/json', function(data){
queryParams['lat'] = data.lat;
queryParams['long'] = data.lon;
callApi('towns', queryParams, '').then((res) => {
this.currentCity = res.data.data[0];
})
}.bind(this));
} else {
console.log(this.currentCity);
this.currentCity = list.name
}
localStorage.setItem('town', JSON.stringify(town));
}
а потом его вызывать в первой части кода, но понимаю, что ерунда. Проблема еще в том, что параметр town, метода this.getTownFromLocalStorage() - это значение this.currentCity запроса
$.getJSON('http://ip-api.com/json', function(data){
queryParams['lat'] = data.lat;
queryParams['long'] = data.lon;
callApi('towns', queryParams, '').then((res) => {
this.currentCity = res.data.data[0];
this.getTownFromLocalStorage(this.currentCity);
})
}.bind(this));