Created
June 6, 2021 19:47
-
-
Save jinnkhan88/21113a4c1d493305353e1be43705b388 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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