Skip to content

Instantly share code, notes, and snippets.

@schicks
Created July 13, 2021 20:26
Show Gist options
  • Save schicks/e15d21c44b36667c0dae85b0a8620541 to your computer and use it in GitHub Desktop.
Save schicks/e15d21c44b36667c0dae85b0a8620541 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const maxTime = 5000
const randomSleep = () => {
return new Promise((resolve, reject) => {
const success = maxTime * Math.random()
const failure = maxTime * Math.random() + 100
setTimeout(resolve, success)
setTimeout(reject, failure)
})
}
const fetchMachine = Machine({
id: 'fetch',
initial: 'loading',
context: {
retries: 0
},
states: {
loading: {
invoke: {
id: 'getResults',
src: randomSleep,
onDone: 'success',
onError: 'failure'
},
after: {
[maxTime]: 'failure'
}
},
success: {
type: 'final'
},
failure: {
type: 'final'
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment