Skip to content

Using promises and exports in an atom #156

Answered by bowheart
erictompkins asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @erictompkins Your code looks fine to me. There's nothing wrong necessarily with kicking off promises in injectMemo. Assuming consumers wait for the set promise before using uiAtom, there wouldn't be a race condition between the promise and the exports.

You're right, I don't see an example in the docs of using exports to manipulate the store returned by injectPromise. Here's how I would have written your code using injectPromise:

const initialState: UiData = {
  section: 'unknown',
  sidebarOpen: true,
  sidebarTheme: 'nileBlue',
  theme: 'saphire',
}

const uiAtom = atom('uiAtom', () => {
  const promiseApi = injectPromise<UiData>(
    async () => {
      const path = window.location.p…

Replies: 2 comments 7 replies

Comment options

You must be logged in to vote
1 reply
@erictompkins
Comment options

Answer selected by erictompkins
Comment options

You must be logged in to vote
6 replies
@bowheart
Comment options

@erictompkins
Comment options

@bowheart
Comment options

@erictompkins
Comment options

@bowheart
Comment options

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