0
0
Vueframework~20 mins

Suspense for async components in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Suspense Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Vue Suspense component render initially?

Consider this Vue 3 component using <Suspense> with an async child component. What will the user see first when this component is mounted?

Vue
<template>
  <Suspense>
    <template #default>
      <AsyncChild />
    </template>
    <template #fallback>
      <p>Loading...</p>
    </template>
  </Suspense>
</template>

<script setup>
import { defineAsyncComponent } from 'vue'

const AsyncChild = defineAsyncComponent(() =>
  new Promise(resolve => {
    setTimeout(() => {
      resolve({ template: '<p>Async content loaded</p>' })
    }, 2000)
  })
)
</script>
AThe text 'Loading...' is shown immediately, then replaced by 'Async content loaded' after 2 seconds.
BThe text 'Async content loaded' appears immediately without delay.
CNothing is shown until the async component finishes loading after 2 seconds.
DAn error message is displayed because async components cannot be used inside Suspense.
Attempts:
2 left
💡 Hint

Think about what the fallback slot in <Suspense> is for.

state_output
intermediate
1:30remaining
What is the value of isPending during async component loading?

In Vue 3, when using defineAsyncComponent with Suspense, you can track loading state with isPending. What is the value of isPending while the async component is still loading?

Vue
<script setup>
import { defineAsyncComponent, ref } from 'vue'

const isPending = ref(false)

const AsyncComp = defineAsyncComponent({
  loader: () => new Promise(resolve => setTimeout(() => resolve({ template: '<p>Done</p>' }), 3000)),
  onPending() { isPending.value = true },
  onResolve() { isPending.value = false }
})
</script>
Anull
Bfalse
Cundefined
Dtrue
Attempts:
2 left
💡 Hint

Check what onPending does in defineAsyncComponent.

📝 Syntax
advanced
2:30remaining
Which option correctly uses Suspense with multiple async components?

Given two async components AsyncA and AsyncB, which code snippet correctly uses <Suspense> to show a fallback while both load?

A
&lt;Suspense&gt;
  &lt;template #default&gt;
    &lt;AsyncA /&gt;
    &lt;AsyncB /&gt;
  &lt;/template&gt;
  &lt;template #fallback&gt;
    &lt;p&gt;Loading both components...&lt;/p&gt;
  &lt;/template&gt;
&lt;/Suspense&gt;
B
&lt;Suspense&gt;
  &lt;AsyncA /&gt;
  &lt;AsyncB /&gt;
  &lt;template #fallback&gt;
    &lt;p&gt;Loading both components...&lt;/p&gt;
  &lt;/template&gt;
&lt;/Suspense&gt;
C
&lt;Suspense fallback="&lt;p&gt;Loading both components...&lt;/p&gt;"&gt;
  &lt;AsyncA /&gt;
  &lt;AsyncB /&gt;
&lt;/Suspense&gt;
D
&lt;Suspense&gt;
  &lt;template #fallback&gt;
    &lt;p&gt;Loading both components...&lt;/p&gt;
  &lt;/template&gt;
  &lt;AsyncA /&gt;
  &lt;AsyncB /&gt;
&lt;/Suspense&gt;
Attempts:
2 left
💡 Hint

Remember the correct slot names for Suspense fallback and default content.

🔧 Debug
advanced
2:30remaining
Why does this Suspense fallback never show?

Look at this Vue 3 code. The fallback slot content never appears even though the async component takes 3 seconds to load. Why?

Vue
<template>
  <Suspense>
    <AsyncComponent />
    <template #fallback>
      <p>Loading...</p>
    </template>
  </Suspense>
</template>

<script setup>
import { defineAsyncComponent } from 'vue'

const AsyncComponent = defineAsyncComponent(() =>
  new Promise(resolve => setTimeout(() => resolve({ template: '<p>Loaded</p>' }), 3000))
)
</script>
AThe async component loads instantly, so fallback never shows.
BThe fallback slot is ignored because the async component is not inside a default slot template.
CThe fallback slot must be declared before the async component inside Suspense.
DSuspense requires a <SuspenseBoundary> wrapper to show fallback.
Attempts:
2 left
💡 Hint

Check how slots are structured inside Suspense.

🧠 Conceptual
expert
3:00remaining
How does Vue Suspense handle multiple async dependencies internally?

When a Vue Suspense component wraps multiple async components, how does it decide when to switch from fallback to default content?

AIt renders each async component as soon as it resolves, replacing fallback piece by piece.
BIt shows fallback until the first async component resolves, then switches to default content immediately.
CIt waits for all async components inside the default slot to resolve before rendering the default content.
DIt randomly picks one async component to wait for before switching from fallback.
Attempts:
2 left
💡 Hint

Think about the user experience when multiple async components load.