Skip to content

Instantly share code, notes, and snippets.

@grabbou
Created October 19, 2015 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grabbou/b88847cf20298596cc5e to your computer and use it in GitHub Desktop.
Save grabbou/b88847cf20298596cc5e to your computer and use it in GitHub Desktop.
// will be caught by promise-middleware
function makeASandwichWithSecretSauce(name) {
return {
types: [],
payload: fetch(...)
}
};
// This is complex one
// BTW, you don't have redux-thunk
// our injectDependencies middleware does that anyway
// because steida wanted it! Check it out
function makeSandwichesForEverybody() {
return ({dispatch, getState}) => {
if (!getState().sandwiches.isShopOpen) {
// You don’t have to return Promises, but it’s a handy convention
// so the caller can always call .then() on async dispatch result.
return Promise.resolve();
}
return dispatch(
makeASandwichWithSecretSauce('My Grandma')
).then(() =>
Promise.all([
dispatch(makeASandwichWithSecretSauce('Me')),
dispatch(makeASandwichWithSecretSauce('My wife'))
])
).then(() =>
dispatch(makeASandwichWithSecretSauce('Our kids'))
).then(() =>
dispatch(getState().myMoney > 42 ?
withdrawMoney(42) :
apologize('Me', 'The Sandwich Shop')
)
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment