angularngrxngrx-effects

Will all effects be executed if one of them redirects to external URL?


I have a case where clicking a button dispatches some action. This action is being listened to by two effects:

sendAnalytics$ = createEffect(
  () =>
    this.actions$.pipe(
      ofType(MyActions.ButtonClicked),
      tap(() => {
        this.analyticsSerice.send({timestamp: new Date()})
      })
    ),
  { dispatch: false }
);

navigateToNextStep$ = createEffect(
  () =>
    this.actions$.pipe(
      ofType(MyActions.ButtonClicked),
      tap(() => {
        window.location.href = "https://next-step.com"
      })
    ),
  { dispatch: false }
);

As you can see, the first one just sends some event. The other one though, navigates to another webpage. I think it's a potential race condition, but I'm not sure, knowing that JS runs in a single thread. Does the order of effects creation have any impact on that?

I'd like the analytics event to always be sent, before the user is taken away.


Solution

  • The effects are executed in the order that they're registered. But, that doesn't make sure that the analytics has been send (depending on the implementation). If the request takes a while, it might get canceled when navigating away from the page.