DEV Community

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

Collapse
 
codingdive profile image
Mikey Stengel • Edited

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.

'FETCH_CATS': {
  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 :)