Search code examples
rxjsngrx

how to properly chain dispatch other actions from effect/action


I have an action A (intercepted by an side effect), which triggers two other actions after A is finished processing.

Currently, I'm using the do() operator from rxjs, something like:

this._action$.ofType('TYPE_A').switchMap(action => a_http_call())
    .distinctUntilChanged()
    .do(data => setTimeout(() => this._store.dispatch({type: 'TYPE_B'}), 1))
    .do(data => setTimeout(() => this._store.dispatch({type: 'TYPE_C'}), 1))
    //.some()
    //.more()
    //.process()
    //.done()
    ;

I use the setTimeout() call to (well, hoping) break up the action B from nesting in the middle of A's process. AND, go ahead with A's process after the two do()s

Does this practice well? Is there a preferred pattern for triggering other actions?


Solution

  • This question was about the old RXJS library, and is no longer valid. The proper way nowadays would most likely using pipe().


    The old & obsolete answer just for record.

    Found the native rx way here, which was pointed out by this question