Asynchronous fetch

by Génesis García Morilla

HTML

<div></div>

JavaScript

$div_ = document.querySelector('div')

async function async_fetch(url) {
  let response = await fetch(url)
  if (response.ok) return await response.json()
  else throw new Error(response.status)
}

async_fetch('https://httpbin.org/ip')
  .then(data => $div_.textContent = JSON.stringify(data))
  .catch(error => alert(error))