Skip to content

Instantly share code, notes, and snippets.

@hendrikswan
Last active May 7, 2019 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hendrikswan/463a13f0f3e6a82a0b8c5130f70cd3af to your computer and use it in GitHub Desktop.
Save hendrikswan/463a13f0f3e6a82a0b8c5130f70cd3af to your computer and use it in GitHub Desktop.
A redux thunk middleware example
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