Skip to content

Instantly share code, notes, and snippets.

@baranovxyz
Created May 3, 2020 21:26
Show Gist options
  • Save baranovxyz/9ea2e86d2647f73a49ab84eacb7350b0 to your computer and use it in GitHub Desktop.
Save baranovxyz/9ea2e86d2647f73a49ab84eacb7350b0 to your computer and use it in GitHub Desktop.
function updateState(dispatch) {
const dispatchStatus = status => dispatch(SET_STATUS({ status }));
const dispatchError = e =>
dispatch(SET_STATUS({ status: 'error', message: e.message }));
dispatchStatus('submitting');
return fetch('https://example.com/api/a')
.then(() => {
dispatchStatus('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(() => {
dispatchStatus('finished');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment