Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Promise to Observable in ngrx Effect

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.

like image 741
trigger Avatar asked Oct 22 '25 10:10

trigger


1 Answers

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 ...
like image 192
Julius Dzidzevičius Avatar answered Oct 25 '25 00:10

Julius Dzidzevičius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!