Guest User

Untitled

a guest
Nov 5th, 2023
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function renderFeed() {
  2.     const fragment = document.createDocumentFragment();
  3.     try {
  4.         const feed = await fetch('recent-blog-posts.json').then(x => x.json());
  5.         for (const item of feed) {
  6.             const li = document.createElement('li');
  7.             const a = document.createElement('a');
  8.             li.appendChild(a);
  9.             a.href = item.link;
  10.             a.appendChild(document.createTextNode(item.title));
  11.             fragment.appendChild(li);
  12.         }
  13.     } catch (e) {
  14.         const li = document.createElement('li');
  15.         li.innerHTML = '<i>Failed to fetch recent blogs</i>';
  16.         fragment.appendChild(li);
  17.     }
  18.     return fragment;
  19. }
  20.  
  21. async function attachPosts() {
  22.     const el = document.getElementById("recent_blog_posts");
  23.     if (!el) return;
  24.     el.appendChild(await renderFeed());
  25. }
  26.  
  27. attachPosts();
  28.  
Advertisement
Add Comment
Please, Sign In to add comment