Created
April 30, 2020 16:56
-
-
Save AshleyGrant/8f7cc057e31f94269061c9bf4789e7e2 to your computer and use it in GitHub Desktop.
Using a Resolved Promise Repeatedly
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 lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>GistRun</title> | |
</head> | |
<body> | |
<h1>Hello world!</h1> | |
<input type="number" id="index" /> | |
<button id="displayUser">Display nth User</button> | |
<p> | |
User name: <span id="userName"></span> | |
</p> | |
<script src="script.js"></script> | |
</body> | |
</html> |
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
let fetchUsers; | |
document.getElementById('displayUser').onclick = () => { | |
if(!fetchUsers) { | |
fetchUsers = fetch('https://api.github.com/users') | |
.then( response => response.json()); | |
} | |
const index = document.getElementById('index').value; | |
fetchUsers.then(users => { | |
document.getElementById('userName').innerHTML = users[index].login; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment