Skip to content

Instantly share code, notes, and snippets.

@AshleyGrant
Created April 30, 2020 16:56
Show Gist options
  • Save AshleyGrant/8f7cc057e31f94269061c9bf4789e7e2 to your computer and use it in GitHub Desktop.
Save AshleyGrant/8f7cc057e31f94269061c9bf4789e7e2 to your computer and use it in GitHub Desktop.
Using a Resolved Promise Repeatedly
<!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>
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