Skip to content

Instantly share code, notes, and snippets.

@GuillaumeNachury
Created June 13, 2018 09:46
Show Gist options
  • Save GuillaumeNachury/695d2f4a7b03f0b0227fab23a431f3f8 to your computer and use it in GitHub Desktop.
Save GuillaumeNachury/695d2f4a7b03f0b0227fab23a431f3f8 to your computer and use it in GitHub Desktop.
Redux Async middleware - Add a convient way to dispatch new action from an action
const asyncDispatcMiddleware = store => next => action => {
let syncActivityFinished = false;
let actionQueue = [];
function flushQueue() {
actionQueue.forEach(a => store.dispatch(a));
actionQueue = [];
}
function asyncDispatch(asyncAction) {
actionQueue = actionQueue.concat([asyncAction]);
if (syncActivityFinished) {
flushQueue();
}
}
const actionWithAsyncDispatch = Object.assign({}, action, {asyncDispatch});
next(actionWithAsyncDispatch);
syncActivityFinished = true;
flushQueue();
};
export default asyncDispatcMiddleware;
@DastanSE
Copy link

Hello! Do you know how to setup this middleware on nextjs redux?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment