Как лучше организовать код?

Есть такой код. при ошибке 404 хочу менять placeholder внутри input.
Код рабочий, но я понимаю что так нихрена не красиво.

getRequest(link) {
      let input = document.getElementById('choiseCityName');
      fetch(link).then( response => {
          if (response.ok) {
            input.placeholder = 'Please enter a your city name.';
            input.classList.remove('placeholderred')
            input.classList.add('placeholderOk');
            return response.json();
          }
           else if (response.status == '404')  {
             input.value = '';
             input.classList.add('placeholderred');
             input.placeholder = "Sorry we can not find your city";
          }
          throw new Error(`Error: status ${response.status}.`);
        }).then(response => {
            return this.showWeatherData(response);
          })
        },
  • Вопрос задан
  • 83 просмотра
Решения вопроса 1
profesor08
@profesor08 Куратор тега JavaScript
async function getJson(url) {
    let response = await fetch(url);
    return await response.json();
}

async function someFunc(url) {
  try {
    let json = await getJson(url);
    okFunc(...);
  }
  catch (err) {
    errorFunc(...);
  }
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы