Skip to content

How to Await Promises Stored in State without Losing Reactivity #262

Answered by Tao-VanJS
Kynson asked this question in Q&A
Discussion options

You must be logged in to vote

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 :-)

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@sirenkovladd
Comment options

@Kynson
Comment options

Answer selected by Tao-VanJS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants