0
0
Vueframework~20 mins

Testing async behavior in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Vue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Vue 3 component display after mounting?

Consider this Vue 3 component using the Composition API. It fetches data asynchronously on mount.

import { ref, onMounted } from 'vue';

export default {
  setup() {
    const message = ref('Loading...');

    onMounted(async () => {
      await new Promise(resolve => setTimeout(resolve, 100));
      message.value = 'Data loaded';
    });

    return { message };
  }
}

What text will be shown inside the component immediately after it mounts?

AError: message is not defined
BData loaded
Cundefined
DLoading...
Attempts:
2 left
💡 Hint

Think about when the async code finishes compared to the initial render.

state_output
intermediate
2:00remaining
What is the value of count after this Vue 3 async update?

Given this Vue 3 component snippet:

import { ref, onMounted } from 'vue';

export default {
  setup() {
    const count = ref(0);

    onMounted(() => {
      setTimeout(() => {
        count.value += 1;
      }, 50);
      count.value += 1;
    });

    return { count };
  }
}

What is the value of count.value after 100ms?

A2
B1
C0
DNaN
Attempts:
2 left
💡 Hint

Count how many times count.value is incremented and when.

📝 Syntax
advanced
2:30remaining
Which option correctly handles async data fetching in Vue 3 setup?

Which of these Vue 3 setup functions correctly fetches data asynchronously and updates a reactive variable?

A
setup() {
  const data = ref(null);
  fetchData().then(response => {
    data.value = response;
  });
  return { data };
}
B
setup() {
  const data = ref(null);
  onMounted(async () => {
    data.value = await fetchData();
  });
  return { data };
}
C
setup: async () => {
  const data = ref(null);
  data.value = await fetchData();
  return { data };
}
D
setup() {
  const data = null;
  data = await fetchData();
  return { data };
}
Attempts:
2 left
💡 Hint

Remember that setup cannot be async and reactive variables need to be refs.

🔧 Debug
advanced
2:30remaining
Why does this Vue 3 async update not trigger re-render?

Look at this Vue 3 setup code:

import { reactive, onMounted } from 'vue';

export default {
  setup() {
    const state = reactive({ count: 0 });

    onMounted(() => {
      setTimeout(() => {
        state.count = state.count + 1;
      }, 100);
    });

    return { state };
  }
}

After 100ms, the UI does not update to show the new count. What is the most likely reason?

AThe UI is not bound to state.count in the template, so no update happens.
BThe component did not return the reactive object properly from setup.
CsetTimeout does not work inside onMounted lifecycle hook.
DThe reactive object was mutated incorrectly; should use ref instead of reactive.
Attempts:
2 left
💡 Hint

Check if the template uses the reactive property to display the count.

🧠 Conceptual
expert
3:00remaining
What error occurs if you make Vue 3 setup() async and why?

What happens if you declare the setup function as async in Vue 3, like this?

export default {
  async setup() {
    const data = await fetchData();
    return { data };
  }
}

Choose the correct outcome and explanation.

AVue ignores the async keyword and treats setup as synchronous, causing data to be undefined.
BThe component works fine; Vue waits for the Promise to resolve before rendering.
CVue throws a runtime error because setup must return a plain object, not a Promise.
DVue logs a warning but still renders the component with empty data.
Attempts:
2 left
💡 Hint

Think about what Vue expects from the setup function return value.