Why does my logging show an empty/missing value after I've loaded the data?
This answer is a stub.
We are still working on writing the answers to the FAQ questions. The answer below is incomplete, but you may still find it useful.
The scenario we're considering here typically looks something like this:
vue
<script setup>
import { onBeforeMount, ref } from 'vue'
const data = ref(null)
onBeforeMount(async () => {
data.value = await fetch(/* ... */)
})
console.log(data.value)
</script>
While the code for the console logging appears to be after the data has loaded, it actually runs before the data loads. There are several reasons for that:
- The
onBeforeMount
hook will run just before the component mounts, not immediately. Mounting occurs just aftersetup
has completed. - The
fetch
request is asynchronous. It returns a promise and that promise won't resolve until the data comes back from the server. - Using
await
is equivalent to usingthen()
on the promise. Thethen()
callback is never called immediately, even if the promise has already resolved. It is deferred using something known as a microtask.
The console logging will run before any of those things have happened.