Skip to content

Instantly share code, notes, and snippets.

@baranovxyz
Created May 3, 2020 21:30
Show Gist options
  • Save baranovxyz/8a373825f6799e4a5ec968b86611ff35 to your computer and use it in GitHub Desktop.
Save baranovxyz/8a373825f6799e4a5ec968b86611ff35 to your computer and use it in GitHub Desktop.
const getResponseJson = async response => response.json();
function updateState(dispatch) {
const dispatchStatus = status => dispatch(SET_STATUS({ status }));
const dispatchError = e =>
dispatch(SET_STATUS({ status: 'error', message: e.message }));
const dispatchState = data => dispatch(SET_STATE({ data }));
dispatchStatus('submitting');
return fetch('https://example.com/api/a')
.then(() => {
dispatchStatus('processing');
return fetch('https://example.com/api/b')
.then(getResponseJson)
.then(dispatchState)
.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