Skip to content

Instantly share code, notes, and snippets.

@xralphack
Created April 6, 2020 06:59

Revisions

  1. xralphack created this gist Apr 6, 2020.
    39 changes: 39 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    Machine({
    id: 'SWAPI',
    initial: 'idle',
    context: {
    user: null
    },
    states: {
    idle: {
    on: {
    FETCH: 'loading'
    }
    },
    loading: {
    invoke: {
    id: 'fetchLuke',
    src: (context, event) => fetch('https://swapi.co/api/people/1')
    .then(data => data.json()),
    onDone: {
    target: 'resolved',
    actions: assign({
    user: (_, event) => event.data
    })
    },
    onError: 'rejected'
    },
    on: {
    CANCEL: 'idle'
    }
    },
    resolved: {
    type: 'final'
    },
    rejected: {
    on: {
    FETCH: 'loading'
    }
    }
    }
    })