-
Hello, const latitude = state(52.52);
const longitude = state(13.41);
const data = derive(async () => {
const response = await fetch(
`${API_ROUTE}?latitude=${latitude.val}&longitude=${longitude.val}`
);
const { result } = await response.json();
return result.foo;
}); I've tried the followings but none of them worked: p((await data.val)) Does not work as it eval to the actual value contained in the promise, van can't recognise it as a state object. The value does not update even p(async () => (await data.val)) Does not work, van converted the function into string, text " Rationale: May I know is there a way to achieve this or I have missed anything? Many thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I think you can try this: const latitude = state(52.52);
const longitude = state(13.41);
const data = state()
derive(async () => {
const response = await fetch(
`${API_ROUTE}?latitude=${latitude.val}&longitude=${longitude.val}`
);
const { result } = await response.json();
data.val = result.foo;
}); and then p(data) For more advanced usage of derived states, you can refer to this section: https://vanjs.org/advanced#advanced-state-derivation. Hope it works :-) |
Beta Was this translation helpful? Give feedback.
-
These answers confused me, and also AI answers confused me even further. Is it really necessary to combine async functions inside Derive or html components? Why not just use p(data.val), and create a normal async function (which does not handle any html), and let the async function just update data.val? One thing I noticed, is that you should not call the async function from a component that uses the same state variable as the async function. Of course you need to trigger the async function from somewhere else. |
Beta Was this translation helpful? Give feedback.
I think you can try this:
and then
For more advanced usage of derived states, you can refer to this section: https://vanjs.org/advanced#advanced-state-derivation.
Hope it works :-)