You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Sep 17, 2022. It is now read-only.
I'm trying to realize what useAction should look like for async actions. Say for some reasons I don't want to use await statement for any Promises inside useAction to finally return the state, and instead is looking to somehow update the state asynchronously.
The pattern I have in mind is to return the state from useAction as usual for any state updates before the Promises, and use state = store.getState(); /* update state */; store.setState(state) in Promise then/finally. To some extent, this looks ugly to me, and in case you were thinking on any clear pattern to minimize the code boilerplate and improve readability for async functions, I'll greatly appreciate your feedback.
Let me know if you need a pseudocode or even more concrete TS example.
The text was updated successfully, but these errors were encountered:
If I understand correctly you're trying to understand how Promises work with useAction? useAction supports promises directly.
constperformAsyncOperation=async({ count })=>{// perform some stuff ...return{count: count+1}// here we update the state.}constasyncAction=useAction(performAsyncOperation)asyncAction()// call it or do whatever.// Promise works the same as async just resolve the new state.constsomePromise=({ count })=>newPromise((resolve,reject)=>{resolve({count : count+1})})constaction=useAction(somePromise)action()// call it.
Hey Rahim,
To continue developit/unistore#136 (comment), here is the problem I'm currently facing with async functions and
useAction
.I'm trying to realize what
useAction
should look like for async actions. Say for some reasons I don't want to useawait
statement for any Promises insideuseAction
to finally return the state, and instead is looking to somehow update the state asynchronously.The pattern I have in mind is to return the state from
useAction
as usual for any state updates before the Promises, and usestate = store.getState(); /* update state */; store.setState(state)
in Promisethen/finally
. To some extent, this looks ugly to me, and in case you were thinking on any clear pattern to minimize the code boilerplate and improve readability for async functions, I'll greatly appreciate your feedback.Let me know if you need a pseudocode or even more concrete TS example.
The text was updated successfully, but these errors were encountered: