Skip to content

How to access/pass data field of Store that was initialized Asynchronously #536

Closed Answered by dai-shi
R2rho asked this question in Q&A
Discussion options

You must be logged in to vote

Okay, so if you put a promise in a proxy state, it's just a promise. Even if the promise is resolved, what you get is the promise, not the resolved value.

const state = proxy({ promise: Promise.resolve({ name: 'hello' }) })
state.promise // is a promise
state.promise.name // is thus `undefined`

When you use useSnapshot (or snapshot), you get the resolved value (or thrown if it's not ready).

const state = proxy({ promise: Promise.resolve({ name: 'hello' }) })
  const snap = useSnapshot(state)
  snap.promise // is *not* a promise
  snap.promise.name // is 'hello'

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@R2rho
Comment options

@R2rho
Comment options

@dai-shi
Comment options

@R2rho
Comment options

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