Thuta Learning
Vue.js
AdvancedWeb DevelopmentBeginner

Fetching Data and Async UI

Vue.jsLesson 1925 min

What you'll walk away with

  • Make an async request
  • Show loading/error/empty states
  • Cancel a request on unmount

Explain it simply

Fetching Data and Async UI, in plain language

Every network request needs more than just a success path — it needs loading, error, and empty states too. Check response.ok and use try/catch/finally. If the component unmounts, you can use an AbortController to cancel a request you no longer need.

Problem → Why → Solution

The problem — why this concept matters

Not checking response.ok

A common mistake

Not setting the loading state back to false when an error occurs

Core concept — understand how it works

Build a solid data UI using fetch, async/await, and loading, error, and cancellation states.

Mental model
  1. props or event
  2. component and template
  3. rendered Vue UI

Make an async request

Core syntax or pattern

vue
<script setup>
import { ref, onMounted } from 'vue'
const users = ref([])
const loading = ref(true)
const error = ref('')

onMounted(async () => {
  try {
    const response = await fetch('/api/users')
    if (!response.ok) throw new Error('Request failed')
    users.value = await response.json()
  } catch (cause) {
    error.value = cause instanceof Error ? cause.message : 'Unknown error'
  } finally {
    loading.value = false
  }
})
</script>

Build a solid data UI using fetch, async/await, and loading, error, and cancellation states.

Read the code step by step

  1. props or event<script setup>

    Every network request needs more than just a success path — it needs loading, error, and empty states too. Check response.ok and use try/catch/finally. If the component unmounts, you can use an AbortController to cancel a request you no longer need.

  2. component and templateif (!response.ok) throw new Error('Request failed')

    Build a solid data UI using fetch, async/await, and loading, error, and cancellation states.

  3. rendered Vue UI</script>

    Apply Fetching Data and Async UI in a small example and test normal, edge, and failure cases.

Execution flow
  1. props or event
  2. component and template
  3. rendered Vue UI

Success

Make an async request

Failure

Not checking response.ok

Let's break it down simply

Every network request needs more than just a success path — it needs loading, error, and empty states too. Check response.ok and use try/catch/finally. If the component unmounts, you can use an AbortController to cancel a request you no longer need.

vue
<script setup>
import { ref, onMounted } from 'vue'
const users = ref([])
const loading = ref(true)
const error = ref('')

onMounted(async () => {
  try {
    const response = await fetch('/api/users')
    if (!response.ok) throw new Error('Request failed')
    users.value = await response.json()
  } catch (cause) {
    error.value = cause instanceof Error ? cause.message : 'Unknown error'
  } finally {
    loading.value = false
  }
})
</script>
You should see
loading → users list OR error message

Try it yourself

Fetch data from a public API and show all four states: loading, error, empty, and success.

Vue — Scaling Up — Vue.js

Real-world usage

How to apply it in a real project

Apply Fetching Data and Async UI in a small example and test normal, edge, and failure cases.

Mini challenge

Build it yourself

Fetch data from a public API and show all four states: loading, error, empty, and success.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
vue
<script setup>
import { ref, onMounted } from 'vue'
const users = ref([])
const loading = ref(true)
const error = ref('')

onMounted(async () => {
  try {
    const response = await fetch('/api/users')
    if (!response.ok) throw new Error('Request failed')
    users.value = await response.json()
  } catch (cause) {
    error.value = cause instanceof Error ? cause.message : 'Unknown error'
  } finally {
    loading.value = false
  }
})
</script>

Expected: Make an async request

Show hint

Apply Fetching Data and Async UI in a small example and test normal, edge, and failure cases.

Quick quiz 1 / 3

What best describes the core idea of Fetching Data and Async UI?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • props or event → component and template → rendered Vue UI
  • Build a solid data UI using fetch, async/await, and loading, error, and cancellation states.
  • Not checking response.ok
  • Apply Fetching Data and Async UI in a small example and test normal, edge, and failure cases.

Vue — Scaling Up — Vue.js

Easy traps

  • Not checking response.ok
  • Not setting the loading state back to false when an error occurs

Exercise

Fetch data from a public API and show all four states: loading, error, empty, and success.

You'll know it worked when: loading → users list OR error message

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Fetching Data and Async UI | Thuta Learning