Logo LIBR
Halo

Portfolio

Back to Blog
9 min read
JS Module 6 — SolutionsJan 2026

Asynchronous JavaScript — Solutions

JavaScript, Async, Fetch API, Solutions
Syifa Fauziyah Arizal

Async is not about speed. It's about control, UX, and resilience.

These solutions focus on improving user experience when working with APIs. A responsive interface, proper error handling, and reusable logic are what separate demos from real applications.

Challenge 1 — Loading & Error Handling

Goals:

  • Show loading text
  • Handle errors properly
JavaScript
const statusText = document.querySelector("#status");
const card       = document.querySelector(".card");

async function fetchUser() {
  statusText.textContent = "Loading...";
  card.innerHTML = "";

  try {
    const res = await fetch("https://randomuser.me/api/");
    if (!res.ok) throw new Error("Network error");

    const data = await res.json();
    const user = data.results[0];

    card.innerHTML = `
      <img src="${user.picture.large}">
      <h3>${user.name.first} ${user.name.last}</h3>
      <p>${user.email}</p>
    `;

    statusText.textContent = "";
  } catch (error) {
    statusText.textContent = "Failed to load data";
  }
}

fetchUser();

Loading state keeps users calm. Error handling keeps apps alive.

Challenge 2 — Refetch with Button

Goal:

  • Click button → fetch new data
JavaScript
const refreshBtn = document.querySelector("#refreshBtn");

refreshBtn.addEventListener("click", () => {
  fetchUser();
});

Same logic. Different trigger.

Challenge 3 — Filter User by Gender

Goal:

  • Fetch based on selected gender
  • Render filtered data
JavaScript
const genderSelect = document.querySelector("#gender");

async function fetchUserByGender() {
  const gender = genderSelect.value;
  statusText.textContent = "Loading...";
  card.innerHTML = "";

  try {
    const res = await fetch(
      `https://randomuser.me/api/?gender=${gender}`
    );

    const data = await res.json();
    const user = data.results[0];

    card.innerHTML = `
      <img src="${user.picture.large}">
      <h3>${user.name.first} ${user.name.last}</h3>
      <p>${user.email}</p>
      <span class="badge">${user.gender}</span>
    `;

    statusText.textContent = "";
  } catch {
    statusText.textContent = "Something went wrong";
  }
}

genderSelect.addEventListener(
  "change",
  fetchUserByGender
);

Now your app reacts to user input and API data. This is dynamic UI.

Why This Module Is a Big Deal

  • You handled async state
  • You protected UI from failures
  • You reused logic efficiently
  • You filtered real API data

This is no longer tutorial-level code. This is production mindset.

Final Takeaways

  • Always show loading feedback
  • Never trust the network
  • Refetching is just reusing functions
  • Filtering data = better UX
  • Async mastery = framework ready

More to read