How to wait for 2 Actions in @ngrx/effects How to wait for 2 Actions in @ngrx/effects typescript typescript

How to wait for 2 Actions in @ngrx/effects


I am new to RXJS but what about this.

You can remove {dispatch: false} if you change the tap to a switchMap.

@Effect({dispatch: false})public waitForActions(): Observable<any> {    const waitFor: string[] = [        SomeAction.EVENT_1,        SomeAction.EVENT_2,        SomeAction.EVENT_3,    ];    return this._actions$        .pipe(            ofType(...waitFor),            distinct((action: IAction<any>) => action.type),            bufferCount(waitFor.length),            tap(console.log),        );}


Using Observable.combineLatest works for me.

@Effect()  complete$ = this.actions$.ofType<Action1>(ACTION1).combineLatest(this.actions$.ofType<Action2>(ACTION2),    (action1, action2) => {      return new Action3();    }  ).take(1);

take(1) results in dispatching Action3() only once.


This worked for me in ngrx 8

waitFor2Actions$ = createEffect(() =>    combineLatest([      this.actions$.pipe(ofType(actions.action1)),      this.actions$.pipe(ofType(actions.action2)),    ]).pipe(      switchMap(() => ...),    )  );