asyncFlow: use async/await to build asyncAction #1806
-
Use Purpose
Example
Implementationhttps://gist.github.com/ZeekoZhu/feb561b0e8203e3faf15208f794d5245 |
Beta Was this translation helpful? Give feedback.
Answered by
EmilTholin
Oct 16, 2021
Replies: 1 comment
-
Hey @ZeekoZhu! Very interesting. As an alternative, you can get typesafe asynchronous flows with " import { flow, types, toGenerator } from "mobx-state-tree";
type Stuff = { id: string; name: string };
function fetchStuff(): Promise<Stuff[]> {
return new Promise((resolve) => {
resolve([
{ id: "1", name: "foo" },
{ id: "2", name: "bar" }
]);
});
}
const Thing = types.model({
id: types.identifier,
name: types.string
});
const ThingStore = types
.model({
things: types.array(Thing)
})
.actions((self) => ({
fetchThings: flow(function* () {
// "stuff" is now of type "Stuff[]"!
const stuff = yield* toGenerator(fetchStuff());
self.things.replace(stuff);
})
})); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
ZeekoZhu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @ZeekoZhu!
Very interesting. As an alternative, you can get typesafe asynchronous flows with "
async/await
" (yield*
) without the need for "utility" actions by doing this: