-
-
Notifications
You must be signed in to change notification settings - Fork 589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to await an async writable atom from another async writable atom #685
Comments
This is the expected behavior but we are not happy with it. I made it return a promise once before v1. It didn't feel like a right abstraction, as it returns promise conditionally. As I understand your expectation, I would like to revisit this along with #607. Meanwhile, the workaround I can think of is the use of traditional callback. const fooAtom = atom(null, async (get, set, callback) => {
await resolveAfter2Seconds();
console.log("END fooAtom write (should be 3rd)");
callback();
});
const barAtom = atom(null, (get, set) => {
set(fooAtom, () => {
console.log("END barAtom write (should be 4th)");
});
}); But, this is ugly... |
i agree that having const [, triggerAction] = useAtom(triggerActionAtom);
const [isActionComplete, setIsActionComplete] = useState(false);
const handleClick = useCallback(() => {
const executeAction = async () => {
await triggerAction();
setIsActionComplete(true);
};
executeAction();
}, [triggerAction]); but now i’m thinking that considering that making an async write-only atom means you are actually returning a value ( function resolveAfter2Seconds() {
console.log("START resolveAfter2Seconds (should be 1st)");
return new Promise((resolve) => {
setTimeout(function () {
resolve("slow");
console.log("END resolveAfter2Seconds (should be 2nd)");
}, 2000);
});
}
const fooAtom = atom((get) => async () => {
await resolveAfter2Seconds();
console.log("END fooAtom read (should be 3rd)");
});
const barAtom = atom((get) => async () => {
await get(fooAtom)();
console.log("END barAtom read (should be 4th)");
}); it seems to work, and using a thunk means that the returned function can take arguments. however, not being able to invoke |
Yea, it's the same problem (and the same fix). Note I will work on it, and will ask you to test it. |
i am using jotai for my auth flow and have a number of async writable (write-only) atoms. some of them depend on other async writable atoms. i tried to use
await set(otherAsyncAtom);
to trigger the other async atom and wait for it to complete, but execution continues immediately (no awaiting).here’s a simple (contrived) demonstration of the issue, using
console.log
to show when each part of the async writable atoms execute: https://codesandbox.io/s/jotai-await-async-set-2esbbis this the expected behavior? and if so, is there a different way to accomplish what i am looking for? thanks for all of your assistance and for an excellent library!
The text was updated successfully, but these errors were encountered: