Skip to content

Instantly share code, notes, and snippets.

@umayr
Created December 1, 2016 21:43
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save umayr/4fe94741caa9339292e80e960aa19ff9 to your computer and use it in GitHub Desktop.
Save umayr/4fe94741caa9339292e80e960aa19ff9 to your computer and use it in GitHub Desktop.
Multiple API calls with Redux Thunks.
function doSomething() {
return dispatch =>
fetch(
'/api/something'
).then(
response => response.json()
).then(
json => dispatch({ type: DO_SOMETHING, json }),
err => dispatch({ type: SOMETHING_FAILED, err })
);
}
function doSomethingElse() {
return dispatch =>
fetch(
'/api/something'
).then(
response => response.json()
).then(
json => dispatch({ type: DO_SOMETHING_ELSE, json }),
err => dispatch({ type: SOMETHING_ELSE_FAILED, err })
);
}
store.dispatch(doSomething()).then(() => {
console.log('I did something');
});
Promise.all([
store.dispatch(doSomething()),
store.dispatch(doSomethingElse())
]).then(() => {
console.log('I did everything!');
});
function doEverything() {
return dispatch => Promise.all([
dispatch(doSomething()),
dispatch(doSomethingElse())
]);
}
store.dispatch(doEverything()).then(() => {
console.log('I did everything!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment