21

In my sample Angular 2 application , I am using ngrx/store and ngrx/effects for state management.

Below is one of the function in a component to add a new item.

addAuthor() {
    
    this.store.dispatch(addAuthorAction(this.fg.value));
    console.log('2')        
} 

In the above code this.store.dispatch(addAuthorAction(this.fg.value)); takes care of making an AJAX call to server and adding a new author to database, which is working fine.

And because this.store.dispatch(addAuthorAction(this.fg.value)); is an async action , console.log("2") statement gets executed even before the AJAX call is completed.

My question is , what needs to be modified so that console.log gets executed after store.dispatch is done.

Praveen
  • 55,303
  • 33
  • 133
  • 164
refactor
  • 13,954
  • 24
  • 68
  • 103
  • I do not know, not having used it, but the source code in https://github.com/ngrx/store/blob/master/src/store.ts gives me the impression that you need to subscribe to the store itself. Like `this.store.subscribe(() => console.log(2))`, as the dispatch call does not return a value. – Aluan Haddad Mar 02 '17 at 06:04
  • The best way to do so is to use the state, add some properties like (isLoading, error, success) and add a selector. if you want I can bring more samples – Smaillns Jun 10 '21 at 08:24

5 Answers5

22

Quick answer : You can't.

As you said, dispatch is asynchronous.

What you should do is use @ngrx/effects. It's nearly the same as using addAuthorAction except that instead of calling a function, you "catch" the dispatched actions and do something just after they've been applied by the reducers.

So what I do in general, is that I divide my actions in 3, for example :

  • FETCH_USER

  • FETCH_USER_SUCCESS

  • FETCH_USER_ERROR

  • FETCH_USER is just used to toggle a boolean so I can display a spinner while fetching the user

  • I catch this action from an effect and make an http request to fetch the user

  • If the http response is OK and I have the info I'm looking for, I dispatch from the effect FETCH_USER_SUCCESS with the response as payload, otherwise I dispatch FETCH_USER_ERROR and I toggle the boolean to false (so we can try to fetch him again for example).

So in your example, if you want to console.log something AFTER the FETCH_USER_SUCCESS, just use another effect to catch the FETCH_USER_SUCCESS and do what you want to here.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
maxime1992
  • 22,502
  • 10
  • 80
  • 121
  • You are totally right in this case. But what If say I want to route to a different component instead of just loving something in console. How do I do that? – Karan Patokar May 20 '20 at 09:20
  • Ngrx used to provide actions for the router (go, back, etc). They do not anymore but they've got a recipe where they explain to do it yourself so it's really easy to setup. Look here: https://github.com/ngrx/platform/blob/2cc88857421b74e01b816f99fe28234bbe77ff1f/projects/ngrx.io/content/guide/migration/v4.md#navigation-actions and then you just dispatch a Go action to where you want to navigate :) – maxime1992 May 20 '20 at 09:36
  • 1
    Thanks. That is indeed a great solution. – Karan Patokar May 20 '20 at 11:44
17

Tested with

"@ngrx/core": "^1.2.0",
"@ngrx/effects": "^7.4.0",
"@ngrx/router-store": "^7.4.0",
"@ngrx/store": "^7.4.0",
"@ngrx/store-devtools": "^7.4.0",

This will also work:

import { ofType, Actions } from '@ngrx/effects';

// Constructor
constructor(
   private _actions$: Actions,
   private _store: Store<any>,
) { }

// YOUR METHOD    
this._store.dispatch(<ACTION>);
this._actions$.pipe(ofType(<ACTION_NAME>)).subscribe((data: any) => {
  console.log(data);  // returned state data        
})
Abhay
  • 6,410
  • 4
  • 26
  • 34
7

With ngrx you can listen to actions like this:

constructor(private actionsSubject$: ActionsSubject, private store: Store<AppState>) {}

ngOnInit() {
  this.actionsSubject$.pipe(
    takeUntil(this.unsubscribe$), // optional
    filter((action) => action.type === SimsActionTypes.SimEditedSuccess)
  ).subscribe(({payload}) => {
    console.log(payload)
  )
}

When you dispatch FIRST_ACTION use an effect to make the HTTP request. In the effect, when you have the response back, fire off a SECOND_ACTION with the response as the payload. Then just listen for SECOND_ACTION in your controller .ts file

danday74
  • 52,471
  • 49
  • 232
  • 283
0

Today I have similar issue and I use tap() to solve this. When are you on your effect import tap from "rxjs/operators"

import { tap } from "rxjs/operators";

and next use it in your switchMap after map operation on SuccessAction

this.service.save(option).pipe(
                map((result) => action.saveSuccess({ option: option})),
                tap(() => this.router.navigate(['link'])),
                catchError((error) => of(actions.saveError({ error }))

this code work form me.

-3

As dispatch is asynchronous (fire and forget), we can subscribe to the returned observable object and then execute the next statement once we get hold of observable object.

addAuthor() {
    this.store.dispatch(addAuthorAction(this.fg.value))
    .subscribe(() => {
        console.log('2')
    });           
}

Alternatively,

myObservableObject$: Observable<any>;
this.myObservableObject$ = this.store.dispatch(addAuthorAction(this.fg.value));
this.myObservableObject$.subscribe((response) => {
    console.log('2')
});

The above is per ngxs state management framework. More @ https://www.ngxs.io/advanced/actions-life-cycle#asynchronous-actions

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
vinsinraw
  • 2,003
  • 1
  • 16
  • 18