在 ngrx Effect 中将 Promise 转换为 Observable

tri*_*ger 4 promise redux ngrx angular

我正在使用给出承诺的第三方库,但我需要将其包装到 ngrx 效果的 Observable 中。这个想法是在应用程序成功初始化时调度新操作。但我需要在承诺解决后发送数据。

classOne.promise().then(result =>
  nested.OnemorePromise(result).then(result2 =>
    //(result2) dispatch new action here (result2)
  )
);
Run Code Online (Sandbox Code Playgroud)

我创建了这样的东西:

classOne.promise().then(result =>
  nested.OnemorePromise(result).then(result2 =>
    //(result2) dispatch new action here (result2)
  )
);
Run Code Online (Sandbox Code Playgroud)
它给了我错误 - 效果是调度错误的操作。

更新:

@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)
      )
    );
    })
Run Code Online (Sandbox Code Playgroud)

地图不是函数。

Jul*_*ius 9

您可以from在Rxjs >= 6中使用:

import { from } from 'rxjs';

map(action => {
  from(classOne.promise()).map(result ...
Run Code Online (Sandbox Code Playgroud)

fromPromise在Rxjs <= 5中:

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

map(action => {
  fromPromise(classOne.promise()).map(result ...
Run Code Online (Sandbox Code Playgroud)