Skip to content

Instantly share code, notes, and snippets.

@baranovxyz
Created May 3, 2020 21:20
Show Gist options
  • Save baranovxyz/3fd232e0ed6931bf1907b10e52fe5b6c to your computer and use it in GitHub Desktop.
Save baranovxyz/3fd232e0ed6931bf1907b10e52fe5b6c to your computer and use it in GitHub Desktop.
function updateState(dispatch) {
// helper functions
const dispatchError = e =>
dispatch(SET_STATUS({ status: 'error', message: e.message }));
dispatch(SET_STATUS({ status: 'submitting' }));
return fetch('https://example.com/api/a')
.then(() => {
dispatch(SET_STATUS({ status: 'processing' }));
return fetch('https://example.com/api/b')
.then(response => {
return response
.json()
.then(data => {
dispatch(SET_STATE({ data }));
})
.catch(dispatchError);
})
.catch(dispatchError);
})
.catch(dispatchError)
.finally(() => {
dispatch(SET_STATUS({ status: 'finished' }));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment