如何使 ngrx 效果等待异步函数

How to make an ngrx effect wait for an async function

我正在使用 node-keytar 在 Electron 应用程序中存储令牌。它使用承诺,因此我需要等待承诺解决以获取令牌。

我尝试创建的效果将调用身份验证服务以获取令牌,然后使用 Angular http 调用将该令牌发送到后端 API。这里的问题是在 Effect 中调用服务函数。由于服务功能需要 await 响应 keytar 整个功能必须是 async,但据我所知,没有办法使效果本身与 async关键字。

我应该在这里使用不同的体系结构吗?我试过使用 .then() 并从内部返回成功操作,但这会引发类型错误。

效果(目前有错误Type Observable<{}> is not assignable to type Observable<Action>):

  setAccount$: Observable<Action> = this.actions$.pipe(
    ofType<SetCurrentAccountPending>(AccountActions.ActionTypes.SetCurrentAccountPending),
    switchMap(action => {
      return this.accountService.setCurrentAccount(action.payload).pipe(
        map(
            () => new AccountActions.SetCurrentAccountSuccess(action.payload)
          ),
          catchError(() => {
            return of(new AccountActions.SetCurrentAccountFailure());
          })
        );
    })
  );

服务功能:

async setCurrentAccount(id: string) {
    const password = await AccountHandler.getPasswordFromManager(id);
    const body = {password: password};
    return this.httpClient.post(environment.localApi + '/accounts/' + id, body);
}

这样的事情有帮助吗?

  setAccount$: Observable<Action> = this.actions$.pipe(
    ofType<SetCurrentAccountPending>(AccountActions.ActionTypes.SetCurrentAccountPending),
    switchMap(action => this.accountService.setCurrentAccount(action.payload)),
    map(data => new AccountActions.SetCurrentAccountSuccess(data)),
    catchError(error => of(new AccountActions.SetCurrentAccountFailure()))
  );