How to make a component wait for an async in a composable? #7517
-
When the app mounts, it runs a function called "initUser"(in a composable) that fetches data and assign it to “loggedInUser.value”.
the same cmposable (named Deskree) has a user object which has a function that just returns the loggedInUser object:
on the user page i want to execute the get function to get the user data:
So the object has the value property, but accessing it returns null. WHY? just to make sure it is an async problem, I tried to use a timeout of 500ms and 5s
I got the desired result after 5s; so the get() function isn't waiting for initUser(); So now i have two questions : |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
when you look at it in the console, you likely look at it after the value has been assigned, so now it works.
You generally would not. You would use a v-if in the component's template to not render the conent in question until the user is present. reactivity will take care of the rest. <div v-if="Deskree.user.get().value"> In advanced scenarios you can await code in |
Beta Was this translation helpful? Give feedback.
when you look at it in the console, you likely look at it after the value has been assigned, so now it works.
You generally would not. You would use a v-if in the component's template to not render the conent in question until the user is present. reactivity will take care of the rest.
In advanced scenarios you can await code in
setup
but that requires using the experimental Suspense component and is u…