Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@JakeGinnivan
Last active September 4, 2019 09:25
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 JakeGinnivan/f19dc907abfcfb98ff72dc0992e5b45d to your computer and use it in GitHub Desktop.
Save JakeGinnivan/f19dc907abfcfb98ff72dc0992e5b45d to your computer and use it in GitHub Desktop.
function dispatch(action) {
// Promise tracker middleware
const nextAction = reduxThunkMiddleware(action)
// Because react-thunk returns the result of the
// thunk, our async function returns a promise
// Which means this statement will be true
if (Promise.resolve(nextAction) === nextAction) {
// Tracking a promise is a side effect of our middleware
track(nextAction)
}
return nextAction
}
function reduxThunkMiddleware(action) {
// If the action is a function, double dispatch
// and do not pass action to next middleware
if (typeof action === 'function') {
// Return the result of the thunk
return action(dispatch, getState)
}
return next(action)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment