Invoking services

and parallel state nodes

Service

is a function returning promise

Services are invoked

by using special                property

invoke

Handling result

{
  invoke: {
    src: (context, event) => {
      // ...promise
    },
    onDone: {
      target: "success",
      actions: "someAction"
    },
    onError: {
      target: "error",
      actions: "someError"
    }
  }
}
  • onDone  for resolved Promise
  • onError  for rejected Promise

Machine can interpret states in parallel

{
  type: "parallel",
  // calling actions or making a transition
  onDone: {},
  states: {
    fetchingStuff: {
      initial: "init",
      states: {
        init: {
          invoke: {
            // invoking async function
            onDone: "ready"
          },
        },
        ready: {
          type: "final",
        },
      },
    },
    fetchingAnotherStuff: {
      initial: "init",
      states: {
        init: {
          invoke: {
            // invoking async function
            onDone: "ready"
          },
        },
        ready: {
          type: "final",
        },
      },
    },
  },
};
  • type is set to
     
  •                 will trigger when parallel states reach out final state

parallel

onDone

coding time!

7. Invoking services

By Kuba Skoneczny

7. Invoking services

  • 157