how to dispatch action and wait for its completion inside epic of redux-observable

305 Views Asked by At

So I am new to "rxjs" and redux-observables (worked with redux-saga before and familiar with the concept of reactive programming).

What I want to know is how can I dispatch an action from an epic, wait for that action to complete and after that keep doing things in the epic, based on the new state that I've got after the action completion.

I will describe my current situation and hopefully it will help:

    export const cartValidationEpic = (action$, state$) =>
  action$.pipe(
    ofType(Actions.CART_VALIDATION_REQUESTED),
    mapTo(action(Actions.CART_REFRESH_REQUESTED)),
    takeUntil(action$.pipe(ofType(Actions.CART_SYNC_RECEIVED))),
    withLatestFrom(state$),
    map(([, updatedState]) => selectCart(updatedState))
    updatedCart => validate(updatedCart)
    )

I'm working on an e-commerce web app, and I want to validate the cart before checkout. I want that the cartValidationEpic will listen to "CART_VALIDATION_REQUESTED", then dispatch an action "CART_REFRESH_REQUESTED" in order to get the updated cart, and I want to make sure that the epic will run the validation function only after the state updated with the updated cart.

Currently while I'm running the app and clicking on the button that trigger the dispatch of "CART_VALIDATION_REQUESTED", all I can see in redux devtools is that the "CART_VALIDATION_REQUESTED" action was dispatched, but nothing dispatched after it.

I wonder what I am missing and if I am in the right direction of solving it.

Thanks for your help.

0

There are 0 best solutions below