Discussion on: State machine advent: Asynchronous code in XState (19/24)

karfau profile image
Christian Bewernitz

Did I understand correctly that onDone and onError are the keys that have to be used for those actions, as in the keys are the convention XState relies on?
And what happens if they are not provided?

codingdive profile image
Mikey Stengel Author

Yes, onDone and onError are properties of XState. They are handling the events of whatever you are invoking. When invoking a promise, the only two events you can have are fulfilled or rejected.

With a normal event, you also specify a target and actions.

  target: 'fetching',
  // optionally actions: []

The event handlers of the service use the exact same syntax to specify the next state and actions to take.

What happens if they are not provided?

onError and onDone are both optional so you can totally omit them for "fire and forget" services.

You can read more about the invoke property here which also explains some properties I haven't covered :)