Last active
September 4, 2019 09:25
-
-
Save JakeGinnivan/f19dc907abfcfb98ff72dc0992e5b45d to your computer and use it in GitHub Desktop.
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 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