Question presented to candidate:
"In one of our pages, a developer wrote const user = await fetchUser(props.id) directly in <script setup>. It works in one place and renders a blank area in another. Explain what top-level await does to a component, what the compiler generates, and the rules that come with it."
What a strong answer should cover:
- A top-level
awaitin<script setup>turns the component'ssetup()into anasyncfunction; the component then needs a<Suspense>ancestor or it renders nothing (Vue warns). - The compiler wraps each awaited expression in
withAsyncContext, which restores the current component instance after theawait, soonMounted,injectand composables still work after it. - A hand-written
async setup()gets no such help: hooks registered after the firstawaitare lost with a warning. setup()runs once per instance: a prop change does not re-run the awaited fetch; use akeyor a watcher.- Errors from the awaited promise go to
onErrorCaptured/app.config.errorHandler; on the server,renderToStringawaits it and sends complete HTML.
Clarifying questions expected:
- "Is there a
<Suspense>above the place where it renders blank?" - "Should the data refresh when
props.idchanges?"
Code / implementation expected: Yes β a component with top-level await inside <Suspense>, ideally showing the compiled output or the hook rule.