Skip to content

Instantly share code, notes, and snippets.

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