Last active
March 30, 2021 10:00
-
-
Save Atibaashraf/00aafd1e95ea02eef5436ded5289d73c to your computer and use it in GitHub Desktop.
What's the redux idiom for waiting for multiple async calls?
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 doEverything() { | |
return dispatch => Promise.all([ | |
dispatch(doSomething()), | |
dispatch(doSomethingElse()) | |
]); | |
} |
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
store.dispatch(doEverything()).then(() => { | |
console.log('I did everything!'); | |
}); |
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
store.dispatch(doSomething()).then(() => { | |
console.log('I did something'); | |
}); |
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
Promise.all([ | |
store.dispatch(doSomething()), | |
store.dispatch(doSomethingElse()) | |
]).then(() => { | |
console.log('I did everything!'); | |
}); |
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
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