Skip to content

Instantly share code, notes, and snippets.

@andrewgordstewart
Created January 14, 2020 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewgordstewart/e230826aecbc9f180aabdf5f73c11b0b to your computer and use it in GitHub Desktop.
Save andrewgordstewart/e230826aecbc9f180aabdf5f73c11b0b to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(Math.floor(Math.random() * 100));
}, 2000)
})
}
const fetchMachine = Machine({
id: 'fetch',
initial: 'idle',
context: {
value: undefined
},
states: {
idle: {
on: {
FETCH: 'loading'
}
},
loading: {
invoke: {
id: 'fetch random number',
src: fetchData,
onDone: [
{
cond: (_, event) => event.data % 2 === 0,
target: 'even'
},
{
cond: (_, event) => event.data % 2 === 1,
target: 'odd'
},
]
},
},
even: {
type: 'final'
},
odd: {
type: 'final'
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment