[Fixed] How to take action when multiple RxJS forkjoins are complete?

Issue

I have two separate Rxjs forkjoins. We always run the first one and the second one might run conditionally.
But we need to take an action when both are complete or when the first one is complete (if the second one is not called).

How can I achieved it?

sub1 = forkjoin([item1, item2])

sub2 = forkjoin([item3, item4])

Solution

You have 2 observables that you want to trigger at the same time. Simplest way is to merge them and concat with the action you want to take at the end:

const action1$ = forkjoin([item1, item2]);
const action2$ = forkjoin([item3, item4]);

concat([
  merge([action1$, action2$]),
  defer(() => {
    // this will happen after action1 and action2 are complete
    return EMPTY;
  }),
]);

Leave a Reply

(*) Required, Your email will not be published