Skip to content

Instantly share code, notes, and snippets.

@pipex
Created June 5, 2020 15:31
Show Gist options
  • Save pipex/2e87e3f8fb468c9eae87c8e6bd1a11e7 to your computer and use it in GitHub Desktop.
Save pipex/2e87e3f8fb468c9eae87c8e6bd1a11e7 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Function that returns a promise
// This promise might resolve with, e.g.,
// { name: 'David', location: 'Florida' }
const doApiAction = deviceId =>
fetch(`url/to/device/${deviceId}`).then(response => response.json());
const machine = Machine({
id: 'device',
initial: 'current',
context: {
deviceId: 42,
result: false,
error: false
},
states: {
current: {
on: {
ACTION: 'waiting'
}
},
waiting: {
invoke: {
id: 'doAction',
src: (context, event) => doApiAction(context.deviceId),
onDone: {
target: 'target',
actions: assign({ result: (context, event) => event.data })
},
onError: {
target: 'current',
actions: assign({ error: (context, event) => event.data })
}
}
},
target: {},
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment