Last active
May 7, 2019 16:54
-
-
Save hendrikswan/463a13f0f3e6a82a0b8c5130f70cd3af to your computer and use it in GitHub Desktop.
A redux thunk middleware example
This file contains 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 loadData() { | |
return (dispatch) => { | |
fetch('/a/fake/url') | |
.then(response => { | |
if (!response.ok) { | |
// dispatching a downstream action | |
return dispatch(requestFailedAction()); | |
} | |
return response.json(); | |
}) | |
.then(data => dispatch(requestSuccessAction(data))) | |
.catch(err => dispatch(requestFailedAction()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment