In my thunk, I have two dispatches in an array called dispatches
which modify the Redux store. Only after both of them have completed (i.e., modified the Redux store), I want to dispatch finalDispatchCall
. I tried putting them both in a Promise
and called then
, but I still see the finalDispatchCall
being made before the secondDispatchCall
has updated the Redux store.
const dispatches = [];
dispatches.push(dispatch(firstDispatchCall());
dispatches.push(dispatch(secondDispatchCall());
Promise.all([...dispatches]).then(() => {
dispatch(finalDispatchCall());
})
.catch(error => {
logger.error(
`Print out error - ${error}`
);
});
Is there a way to make sure a dispatch has completed before calling another dispatch?
EDIT (more details):
The firstDispatchCall
is making an API call (returning fetch
) and dispatching an action in the then
statement to update the redux store. The secondDispatchCall
is also making an API call (returning fetch
) and dispatching two other actions in their then
statements which each make their own API calls and dispatch actions to update the redux store.
I want to wait until all of this is complete before making that finalDispatchCall
.