Created
May 3, 2020 21:26
-
-
Save baranovxyz/9ea2e86d2647f73a49ab84eacb7350b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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