Unwrapped Atom Always Returns undefined on First Read After v2.10 Update #2877
-
After upgrading to v2.10, the first read of an unwrapped atom always fails (returns fallback or const asyncValueAtom = atom(async () => {
await 0;
return "async value";
});
// check if async value already fetched
const checkValueAtom = atom(undefined, (get) => {
const value = get(unwrap(asyncValueAtom));
console.log(value ? "value is ready" : "value is not ready");
});
function App() {
const value = useAtomValue(asyncValueAtom);
const check = useSetAtom(checkValueAtom);
return (
<>
{value}
<button onClick={check}></button>
</>
);
} From the code and test changes, this seems to be an intentional change, not a bug: However, I couldn't find any explanation for this change. This behavior can sometimes cause issues when using |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
@dmaskasky Can you look into it please? |
Beta Was this translation helpful? Give feedback.
-
Yeah, it's intentional.
I'm sorry. It's not described well. The rationale is that our store implementation shouldn't deal with promises. It's the core principle of Jotai v2 actually.
I wonder if this works for your case. const checkValueAtom = atom(undefined, async (get) => {
const value = await get(asyncValueAtom);
console.log(value ? "value is ready" : "value is not ready");
});
I don't plan to change the store api as it's basically a conceptual fix. |
Beta Was this translation helpful? Give feedback.
Yeah, it's intentional.
I'm sorry. It's not described well. The rationale is that our store implementation shouldn't deal with promises. It's the core principle of Jotai v2 actually.
unwrap
doesn't do a magic now. It takes one tick to resolve a promise. So you should basically prepare forundefined
even if it'sPromise.resolve(0)
.I wonder if this works for your case.