Skip to content

Instantly share code, notes, and snippets.

@ronaiza-cardoso
Created May 22, 2020 10:15
Show Gist options
  • Save ronaiza-cardoso/61bf00338a6696d62028beeb046bf606 to your computer and use it in GitHub Desktop.
Save ronaiza-cardoso/61bf00338a6696d62028beeb046bf606 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const fetchCuteAnimals = () => {
return fetch('https://www.reddit.com/r/aww.json')
.then(res => res.json())
.then(data => data.data.children.map(child => child.data))
}
const cuteAnimalsMachine = Machine({
id: 'cuteAnimals',
initial: 'idle',
context: {
cuteAnimals: null,
error: null
},
states: {
idle: {
on: { FETCH: 'loading' }
},
loading: {
invoke: {
id: 'fetchCuteAnimals',
src: fetchCuteAnimals,
onDone: {
target: 'success',
actions: [
assign({
cuteAnimals: (context, event) => event.data
})
]
},
onError: {
target: 'failure',
actions: [
assign({
error: (context, event) => event.data
})
]
}
}
},
success: {
type: 'final'
},
failure: {
on: { RETRY: 'loading' }
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment