Skip to content

Instantly share code, notes, and snippets.

@caub
Created May 22, 2018 17:58
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 caub/d55d87751a096ef176d96507793b1b56 to your computer and use it in GitHub Desktop.
Save caub/d55d87751a096ef176d96507793b1b56 to your computer and use it in GitHub Desktop.
Don't use redux-saga
const promiseMiddleware = store => next => action => {
if (typeof action.then === 'function') {
return Promise.resolve(action).then(next);
} else if (action.value && typeof action.value.then === 'function') {
return Promise.resolve(action.value).then(value => next({ type: action.type, value }));
}
// else, not a promise
try {
return next(action);
} catch (e) {
return Promise.reject(e);
}
};
// dispatch(someQuery())
// dispatch(fechApi('/foos').then(foos => ({type: FOOS, value: foos})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment