title: How to wait for 2 Actions in @ngrx/effects
body: <p>Can effect wait two actions like Promise.all? Example:</p>
  
  <pre><code>@Effect()
  pulic addUser() {
     return this.actions$.ofType(user.ADD)
        .switchMap(() =&gt; {
           return this.userService.add();
        })
        .map(() =&gt; {
           return new user.AddSuccessAction();
        });
  }
  
  @Effect()
  pulic addUserOptions() {
     return this.actions$.ofType(userOptions.ADD)
        .switchMap(() =&gt; {
           return this.userOptionsService.add();
        })
        .map(() =&gt; {
           return new userOptions.AddSuccessAction();
        });
  }
  
  @Effect()
  public complete() {
     return this.actions$.ofType(user.ADD_SUCCESS, userOptions.ADD_SUCCESS)
        // how to make it works like Promise.all ?
        .switchMap(() =&gt; {
           return this.statisticService.add();
        })
        .map(() =&gt; {
           return new account.CompleteAction();
        });
  }
  </code></pre>
  
  <p><strong>UPDATED</strong>
  What I want to achieve is simillar behavior to Promise.all. How to dispatch two effects in parallel, wait until all the effects are resolved, then dispatch a third action. Something like <a href="https://redux-saga.js.org/docs/advanced/RunningTasksInParallel.html" rel="noreferrer">https://redux-saga.js.org/docs/advanced/RunningTasksInParallel.html</a> With promises it was quite obviouse:</p>
  
  <pre><code>Promise.all([fetch1, fetch2]).then(fetch3);
  </code></pre>
  
  <p>Is it possible in ngrx/effects? Or is it a wrong way in ngrx/effects?</p>
  
  <p><strong>ANSWER</strong></p>
  
  <p>There are few options which you can use:</p>
  
  <p><strong>1) Do not use generic actions.</strong></p>
  
  <p>Follow these rules from Myke Ryan's presentation: <a href="https://youtu.be/JmnsEvoy-gY" rel="noreferrer">https://youtu.be/JmnsEvoy-gY</a></p>
  
  <p><strong>Pros</strong>: easier to debug</p>
  
  <p><strong>Cons</strong>: tons of boilerplate and actions</p>
  
  <p><strong>2) Use complex stream with nested actions.</strong></p>
  
  <p>Check this article: <a href="https://bertrandg.github.io/ngrx-effects-complex-stream-with-nested-actions/" rel="noreferrer">https://bertrandg.github.io/ngrx-effects-complex-stream-with-nested-actions/</a></p>
  
  <p>Here is simple example for two actions:</p>
  
  <pre><code>@Effect()
  public someAction(): Observable&lt;Action&gt; {
      return this.actions$.pipe(
          ofType(actions.SOME_ACTION),
          map((action: actions.SomeAction) =&gt; action.payload),
          mergeMap((payload) =&gt; {
              const firstActionSuccess$ = this.actions$.pipe(
                  ofType(actions.FIRST_ACTION_SUCCESS),
                  takeUntil(this.actions$.pipe(ofType(actions.FIRST_ACTION_FAIL))),
                  first(),
              );
  
              const secondActionsSuccess$ = this.actions$.pipe(
                  ofType(actions.SECOND_ACTION_SUCCESS),
                  takeUntil(this.actions$.pipe(ofType(actions.SECOND_ACTION_FAIL))),
                  first(),
              );
  
              const result$ = forkJoin(firstActionSuccess$, secondActionsSuccess$).pipe(
                  first(),
              )
                  .subscribe(() =&gt; {
                      // do something
                  });
  
              return [
                  new actions.FirstAction(),
                  new actions.SecondAction(),
              ];
          }),
      );
  }
  </code></pre>
  
  <p><strong>Pros</strong>: you can achieve what you want</p>
  
  <p><strong>Cons</strong>: complex stream is too complex to support :) looks ugly and may quickly become to hell, observables won't unsubscribe until succes or fail actions, it means that in theory any third-party actions can emit signals to these observables.</p>
  
  <p><strong>3) Use aggregator pattern.</strong></p>
  
  <p>Check Victor Savkin's presentation about State Management Patterns and Best Practices with NgRx: <a href="https://www.youtube.com/watch?v=vX2vG0o-rpM" rel="noreferrer">https://www.youtube.com/watch?v=vX2vG0o-rpM</a></p>
  
  <p>Here is simple example:</p>
  
  <p>First you need to create actions with correlationId param. CorrelationId should be uniq, it may be some guid for example. This ID you will use in your chain of actions to identify your actions.</p>
  
  <pre><code>export class SomeAction implements Action {
      public readonly type = SOME_ACTION;
  
      constructor(public readonly correlationId?: string | number) { }
      // if you need payload, then make correlationId as a second argument
      // constructor(public readonly payload: any, public readonly correlationId?: string | number) { }
  }
  
  export class SomeActionSuccess implements Action {
      public readonly type = SOME_ACTION_SUCCESS;
  
      constructor(public readonly correlationId?: string | number) { }
  }
  
  export class FirstAction implements Action {
      public readonly type = FIRST_ACTION;
  
      constructor(public readonly correlationId?: string | number) { }
  }
  
  export class FirstActionSuccess implements Action {
      public readonly type = FIRST_ACTION_SUCCESS;
  
      constructor(public readonly correlationId?: string | number) { }
  }
  
  // the same actions for SecondAction and ResultAction
  </code></pre>
  
  <p>Then our effects:</p>
  
  <pre><code>@Effect()
  public someAction(): Observable&lt;Action&gt; {
      return this.actions$.pipe(
          ofType(actions.SOME_ACTION),
          mergeMap((action: actions.SomeAction) =&gt; {
              return [
                  new actions.FirstAction(action.corelationId),
                  new actions.SecondAction(action.corelationId),
              ];
          }),
      );
  }
  
  @Effect()
  public firstAction(): Observable&lt;Action&gt; {
      return this.actions$.pipe(
          ofType(actions.FIRST_ACTION),
          switchMap((action: actions.FirstAction) =&gt; {
              // something
              ...map(() =&gt; new actions.FirstActionSuccess(action.correlationId));
          }),
      );
  }
  // the same for secondAction
  
  @Effect()
  public resultAction(): Observable&lt;Action&gt; {
      return this.actions$.pipe(
          ofType(actions.SOME_ACTION),
          switchMap((action: actions.SomeAction) =&gt; {
              const firstActionSuccess$ = this.actions$.pipe(
                  ofType(actions.FIRST_ACTION_SUCCESS),
                  filter((t: actions.FirstActionSuccess) =&gt; t.correlationId === action.correlationId),
                  first(),
              );
  
              const secondActionsSuccess$ = this.actions$.pipe(
                  ofType(actions.SECOND_ACTION_SUCCESS),
                  filter((t: actions.SecondActionSuccess) =&gt; t.correlationId === action.correlationId),
                  first(),
              );
  
              return zip(firstActionSuccess$, secondActionsSuccess$).pipe(
                  map(() =&gt; new actions.resultSuccessAction()),
              )
          }),
      );
  }
  </code></pre>
  
  <p><strong>Pros</strong>: the same as point 2, but no third-party actions.</p>
  
  <p><strong>Cons</strong>: the same as point 1 and 2</p>
  
  <p><strong>4) Do not use effects for API. Use good old services which emulate effects but return Observable.</strong></p>
  
  <p>In you service:</p>
  
  <pre><code>public dispatchFirstAction(): Observable&lt;void&gt; {
      this.store.dispatch(new actions.FirstAction(filter));
  
      return this.service.someCoolMethod().pipe(
          map((data) =&gt; this.store.dispatch(new actions.FirstActionSuccess(data))),
          catchError((error) =&gt; {
              this.store.dispatch(new actions.FirstActionFail());
  
              return Observable.throw(error);
          }),
      );
  }
  </code></pre>
  
  <p>So you can combine it anywhere later, like:</p>
  
  <pre><code>const result1$ = this.service.dispatchFirstAction();
  const result2$ = this.service.dispatchSecondAction();
  
  forkJoin(result1$, result2$).subscribe();
  </code></pre>
  
  <p><strong>5) Use ngxs: <a href="https://github.com/ngxs/store" rel="noreferrer">https://github.com/ngxs/store</a></strong></p>
  
  <p><strong>Pros</strong>: less boilerplate, this feels like angular stuff, it grows fast</p>
  
  <p><strong>Cons</strong>: has got less features than ngrx</p>