2
votes

I'm using third party library which give promises, but i need to wrap it to Observable in ngrx effect. The idea is to dispatch new action when app is initialized successfully. But i need to dispatch the data when the promise is resolved.

classOne.promise().then(result =>
  nested.OnemorePromise(result).then(result2 =>
    //(result2) dispatch new action here (result2)
  )
);

I created something like this:

@Effect()
  initializeValue$: Observable<Action> = this.actions$.pipe(
    ofAction(AppInitializeAction),
    map(action => {
      classOne.promise().then(result =>
      nested.OnemorePromise(result).then(result2 =>
         this.store.dispatch(new Action(result2))
// ideally just - return new Action(result2)
      )
    );
    })
It gives me error - effect is dispatch wrong action.

upd:

 @Effect()
  initializeValue$: Observable<Action> = this.actions$.pipe(
    ofAction(AppInitializeAction),
    map(action => {
      return from(classOne.promise()).map(result => {
         console.log(result)
        })
    })
  );

map is not a function.

2
If you use Rxjs 6, operators are lettable, meaning you should 'pipe' them - from(classOne.promise()).pipe(map(resultJulius

2 Answers

4
votes

You can use from in Rxjs >= 6:

import { from } from 'rxjs';

map(action => {
  from(classOne.promise()).map(result ...

fromPromise in Rxjs <= 5:

import { fromPromise } from 'rxjs/observable/fromPromise';

map(action => {
  fromPromise(classOne.promise()).map(result ...
1
votes

The problem here is that you're not returning an Action, but you're calling store.dispatch manually (just like what your comment says).

To fix this, use from(promise) instead of .promise().then(...).

Also make sure the map function returns something, which is not the case now.