Replies: 1 comment 4 replies
-
I should document managing multiple asynchronous atoms better. Yes. In Zedux, asynchrony is managed outside atom evaluation. This gives you full control over asynchrony at the cost of having to exercise that control. Awaiting An early prototype of Zedux actually made atoms asynchronous like Recoil/Jotai. We got rid of it 'cause we needed this extra control. Btw you can forward promises if you want to use suspense with return api().setPromise(nameInstance.promise) Also with exports, one thing to watch out for is something force-destroying the const otherAtom = atom("other", () => {
const { getInstance } = injectAtomGetters();
getInstance(nameAtom); // register a graph dependency, but don't use it here
return api().setExports({
sayName: async () => {
const nameInstance = getInstance(nameAtom); // get the instance here
await nameInstance.promise;
console.log(nameInstance.getState());
}
});
}); If the atom takes params, it may be more convenient to pass the possibly-destroyed instance directly to Here's an example combining all these tips. In practice, this is up to you. I always guard against force-destruction in case someone else comes along and force-destroys an atom. But you could of course make it a convention in your app to just never force-destroy. You can get along without that feature. |
Beta Was this translation helpful? Give feedback.
-
Is this the preferred way to write it in zedux because i didn't see (or miss) the pattern in docs.
https://codesandbox.io/s/zedux-async-function-orx3sd?file=/src/App.tsx
Beta Was this translation helpful? Give feedback.
All reactions