Last active
July 15, 2019 23:31
-
-
Save miketamis/6b35545f242b80a94c3374c1048b8467 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 fetchUser = userId => new Promise((resolve, reject) => { | |
setTimeout(() => { | |
reject({'name': "Mike"}) | |
}, 1000) | |
}); | |
const userMachine = Machine({ | |
id: 'user', | |
initial: 'idle', | |
context: { | |
userId: 42, | |
user: undefined, | |
error: undefined | |
}, | |
states: { | |
idle: { | |
on: { | |
FETCH: 'loading' | |
} | |
}, | |
loading: { | |
on: { | |
restart: 'loading' | |
}, | |
invoke: { | |
id: 'getUser', | |
src: (context, event) => fetchUser(context.userId), | |
onDone: { | |
target: 'success', | |
actions: assign({ user: (context, event) => event.data }) | |
}, | |
onError: { | |
target: 'failure', | |
actions: assign({ error: (context, event) => event.data }) | |
} | |
} | |
}, | |
success: {}, | |
failure: { | |
on: { | |
RETRY: 'loading' | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment