Skip to content

asyncFlow: use async/await to build asyncAction #1806

Answered by EmilTholin
ZeekoZhu asked this question in Ideas
Discussion options

You must be logged in to vote

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:

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 "St…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ZeekoZhu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants