-
Notifications
You must be signed in to change notification settings - Fork 644
A better async/await actions usage? #1415
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
Comments
Not sure if I understood right but this runInAction wrapping is already done if you use flow. |
😨... |
Yes, but as mentioned in the docs, it's as simple as replacing The generator version of this action in the docs is shorter and simpler than the one using async+ Imagine you have an This also breaks any notion of using actions as atomic transactions; see this sandbox. I'm not sure why you would insist on using async/await inside the action... you can still use it as much as you want outside: // in the model actions:
fetchAThing: flow(function* fetchAThing(thingId) {
const thingFromServer = yield fetchThingFromServer(thingId)
self.thing = thingFromServer
})
// ... somewhere outside the model:
async function fetchThingFromServer(thingId) {
const response = await callTheServer(thingId)
const thing = extractThing(response)
return thing
} |
@beaulac Thank you very much for your answers and examples. |
Also something worth noting is that unlike |
Great point – it seems TS is a ways off inferring intermediate yields despite the improvements in 3.6. As an alternative, it might be worth looking at |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions. |
Question
I have read Creating asynchronous actions. The main reason we can't use async/await is that modification operations to state tree in async/await function cann't be detech automatically. so, i think we can write like below to continue use async/await by explicitly wrap operations in a action.I am not sure if there are potential side effects? If it's ok, i hope MST can provider natively the
runInAction
interface to the model typeThe text was updated successfully, but these errors were encountered: