Skip to content

Async atom that suspends the first time, returns previous value subsquently #2228

Discussion options

You must be logged in to vote

Thanks unwrap as you described did the trick! It's much simpler now, and works:

Here's the important part:

export const selectAtomWithSwr = <T>(
  selector: Selector<T>,
  _areEqual: AreEqual<T> = Object.is // TODO
): Atom<T | Promise<T>> => {
  const baseAtom = atom(get => selector(get));
  const unwrappedAtom = unwrap(baseAtom, prev => prev);

  return atom(get => get(unwrappedAtom) ?? get(baseAtom));
}

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@stevemolitor
Comment options

Answer selected by stevemolitor
@stevemolitor
Comment options

@scamden
Comment options

@sebinsua
Comment options

@alirezamirian
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants