Created
December 1, 2016 21:43
-
-
Save umayr/4fe94741caa9339292e80e960aa19ff9 to your computer and use it in GitHub Desktop.
Multiple API calls with Redux Thunks.
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 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