Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Atibaashraf/00aafd1e95ea02eef5436ded5289d73c to your computer and use it in GitHub Desktop.
Save Atibaashraf/00aafd1e95ea02eef5436ded5289d73c to your computer and use it in GitHub Desktop.
What's the redux idiom for waiting for multiple async calls?
function doEverything() {
return dispatch => Promise.all([
dispatch(doSomething()),
dispatch(doSomethingElse())
]);
}
store.dispatch(doEverything()).then(() => {
console.log('I did everything!');
});
store.dispatch(doSomething()).then(() => {
console.log('I did something');
});
Promise.all([
store.dispatch(doSomething()),
store.dispatch(doSomethingElse())
]).then(() => {
console.log('I did everything!');
});
import 'babel-core/polyfill'; // so I can use Promises
import fetch from 'isomorphic-fetch'; // so I can use fetch()
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 })
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment