Skip to content

Instantly share code, notes, and snippets.

@jinnkhan88
Created June 6, 2021 19:47
Show Gist options
  • Save jinnkhan88/21113a4c1d493305353e1be43705b388 to your computer and use it in GitHub Desktop.
Save jinnkhan88/21113a4c1d493305353e1be43705b388 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const fetchAnimals = ()=>{
return fetch('https://www.reddit.com/r/aww.json')
.then(res => res.json())
.then(data => data.data.children.map(child => child.data))
}
const cuteAnimals = Machine({
id:'cuteAnimals',
initial:'idle',
context:{
cuteAnimals:null,
error:null
},
states:{
idle:{
on:{
FETCH:'loading'
}
},
loading:{
invoke:{
id:'fetchAnimals',
src:fetchAnimals,
onDone:{
target:'success',
actions:assign({
cuteAnimals:(ctx,event)=>event.data
})
},
onError:{
target:'failure',
actions:assign({
error:(ctx,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