cassiozen / useStateMachine

The <1 kb state machine hook for React
MIT License
2.38k stars 47 forks source link

Allow usage of async when returning void in an effect #77

Closed rjdestigter closed 2 years ago

rjdestigter commented 3 years ago

This allows usage of async in combination with void effects. E.g.:

useStateMachine({
  initial: "downloading",
  states: {
    idle: {
      on: {
        DOWNLOAD: "downloading",
      },
    },
    downloading: {
      on: {
        OK: "idle",
      },
      async effect({ setContext }) {
       await Promise.resolve(42)
       setContext({..}).send(..)
      },
    },
  },
});
rjdestigter commented 3 years ago

Just saw your codesandbox example. I think I might like that better actually.