Skip to content

Instantly share code, notes, and snippets.

@irwingb1979
Last active November 14, 2022 20:55
Show Gist options
  • Save irwingb1979/a3bac78eb08e16b7d833fbd43360eea0 to your computer and use it in GitHub Desktop.
Save irwingb1979/a3bac78eb08e16b7d833fbd43360eea0 to your computer and use it in GitHub Desktop.
Async Await - Try Catch
const content = document.querySelector('#content');
async function fetchUsers() {
try {
const users = await fetch('https://jsonplaceholder.typicode.com/users');
const usersJson = await users.json();
console.log(usersJson);
showHtml(usersJson);
}
catch(err) {
console.log(err);
console.log('Hubo un errror');
}
}
fetchUsers();
function showHtml(users) {
users.forEach(element => {
content.innerHTML += `
<div class="user">
<h3>${element.name}</h3>
<p>${element.username}</p>
<p>${element.email}</p>
</div>
`
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment