Skip to content

Instantly share code, notes, and snippets.

@40thieves
Created August 4, 2021 09:49
Show Gist options
  • Save 40thieves/8132a431e8d5515ec327cf7d70e54342 to your computer and use it in GitHub Desktop.
Save 40thieves/8132a431e8d5515ec327cf7d70e54342 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const fetchMachine = Machine({
id: 'http',
initial: 'idle',
context: {
data: []
},
states: {
idle: {
on: {
FETCH: 'loading'
}
},
loading: {
invoke: {
src: invokeFetchData,
onDone: {
target: 'success',
actions: assign({
data: (context, event) => {
return [...context.data, event.data]
}
})
}
}
},
success: {
type: 'final'
}
}
});
function invokeFetchData() {
return new Promise((resolve) => {
setTimeout(() => {
resolve('Hello world!');
}, 3000);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment