Skip to content

Instantly share code, notes, and snippets.

@kpliuniverse
Created August 23, 2026 10:37
Show Gist options
  • Select an option

  • Save kpliuniverse/6e79f8e98cbb4efaefb3e9b3ffd80d7e to your computer and use it in GitHub Desktop.

Select an option

Save kpliuniverse/6e79f8e98cbb4efaefb3e9b3ffd80d7e to your computer and use it in GitHub Desktop.
Event-driven programming async
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h2>Posts</h2>
<h3 id="loading">Loading posts...</h3>
<h3 id="posts"></h3>
<script>
const loading = document.getElementById("loading");
const posts = document.getElementById("posts");
const getData = fetch("https://jsonplaceholder.typicode.com/posts");
getData
.then((response) => response.json())
.then((data) => {
loading.style.display = "none";
data.slice(0, 10).forEach((post) => {
posts.innerHTML += `
<div>
<h2>${post.title}</h2>
<p>${post.body}</p>
</div>
`;
});
})
.catch((error) => {
loading.textContent = "Failed to load posts.";
console.log("Error: ", error);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment