我正在使用node-keytar在电子应用程序中存储令牌。它使用Promise,因此我需要等待Promise解析才能获得令牌。
我试图创建的效果将调用身份验证服务来获取令牌,然后使用Angular http调用将令牌发送到后端API。这里的问题是在效果中调用服务函数。因为服务函数需要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);
}发布于 2019-04-09 05:25:50
像这样的东西有帮助吗?
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()))
);https://stackoverflow.com/questions/55580148
复制相似问题