Unexpected behavior when emitting actions synchronously

Author: robmcmCreated Mar 16, 2022Updated Dec 9, 2025
Labelsreviewed

Unexpected behavior when emitting actions synchronously

What is the current behavior? When an epic dispatches an action synchronously, it will in turn synchronously call store.dispatch (https://github.com/redux-observable/redux-observable/blob/master/src/createEpicMiddleware.ts#L85). This means all reducers and epics will be called with the new action before the current action propagates through the remaining combined epics. This causes some confusion where the state$.value can differ from what would be expected (even inside the same state$ pipe).

If the current behavior is a bug, please provide the steps to reproduce and a minimal demo of the problem using JSBin, StackBlitz, or similar.

(action$, state$, dependencies) => {
    const initialState = state$.value; // Initial state
    return state$.pipe(
        tap(state => console.log(state === initialState), // Will return false
        startWith(actionToChangeState) // This action will be run through the reducers before state$ is subscribed to above
   )
}

What is the expected behavior? I would expect the epics to dispatch actions after the current action/state has been passed to each epic in combine epics. However I understand this has probably been around for a while, and may only crop up in more complex use cases (with multiple epics). A workaround is to add delay(1) to the end of any epic that dispatches synchronously (specifically in response to an emit on state$ or action$). If this is unlikely to be fixed it may be good to have it added to a "known issues" section in the docs?

Which versions of redux-observable, and which browser and OS are affected by this issue? Did this work in previous versions of redux-observable? I think it has always been like this.

Source: redux-observable/redux-observable