Last active
June 23, 2017 19:05
-
-
Save robinloeffel/2e36004b042beecd69a631b6af032eb4 to your computer and use it in GitHub Desktop.
Fetch + Async Await (JS) - https://rbnlffl.synology.me/fetchasyncawait/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>fetch and async await</title> | |
<style type="text/css"> | |
html { | |
font-size: 18px; | |
font-family: sans-serif; | |
line-height: 1.6; | |
color: #333; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Suh dud</h1> | |
<p>My contents will be replaced...</p> | |
<script> | |
const getUsers = async (url) => { | |
const req = await fetch(url), | |
res = await req.json(), | |
p = document.querySelector('p'), | |
rand = Math.round(Math.random() * 9); | |
p.innerHTML = `I am <b>${res[rand].name}</b> and my mail is <b>${res[rand].email}</b>.`; | |
}; | |
getUsers('https://jsonplaceholder.typicode.com/users'); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment