首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React,redux,redux observable: Promise resolve类型

React,redux,redux observable: Promise resolve类型
EN

Stack Overflow用户
提问于 2018-03-13 09:37:34
回答 1查看 308关注 0票数 0

我正在尝试定义一个承诺的解决方案的类型。以下是代码的一部分,或者如果你想在github上查找它:https://github.com/Electra-project/Electra-Desktop/blob/master/src/app/header/epics.ts

代码语言:javascript
复制
export function getStakingInfo(action$: ActionsObservable<HeaderActions>, store: any): any {
  return action$.ofType(ActionNames.GET_STAKING_INFO)
    .map(() => store.getState().electra.electraJs) // get electraJs object from the store
    .filter((electraJs: any) => electraJs) // check if electraJs exists
    .map(async (electraJs: any) => electraJs.wallet.getStakingInfo())
    .switchMap(async (promise: Promise<WalletStakingInfo>) => new Promise((resolve) => {
      promise
        .then((data: WalletStakingInfo) => {
          resolve({
            payload: {
              ...data
            },
            type: ActionNames.GET_STAKING_INFO_SUCCESS
          })
        })
        .catch((err: any) => {
          resolve({
            type: ActionNames.GET_STAKING_INFO_FAIL
          })
        })
    }))
    .catch((err: any) =>
      Observable.of({
        type: ActionNames.GET_STAKING_INFO_FAIL
      }))
}

我收到一个错误,上面代码中的resolve不是类型定义的new Promise((resolve) => {。然而,我不确定解决的类型。

有谁能指导我这里应该是什么类型的解决方案?

EN

回答 1

Stack Overflow用户

发布于 2018-03-13 17:51:08

您可以像这样定义您自己的类型,例如:

代码语言:javascript
复制
type Resolve = (action: { payload?: WalletStakingInfo; type: ActionNames; }) => void;

export function getStakingInfo(action$: ActionsObservable<HeaderActions>, store: any): any {
    return action$.ofType(ActionNames.GET_STAKING_INFO)
        .map(() => store.getState().electra.electraJs) // get electraJs object from the store
        .filter((electraJs: any) => electraJs) // check if electraJs exists
        .map(async (electraJs: any) => electraJs.wallet.getStakingInfo())
        .switchMap(async (promise: Promise<WalletStakingInfo>) => new Promise((resolve: Resolve) => {
            promise
                .then((data: WalletStakingInfo) => {
                    resolve({
                        payload: {
                            ...data
                        },
                        type: ActionNames.GET_STAKING_INFO_SUCCESS
                    })
                })
                .catch((err: any) => {
                    resolve({
                        type: ActionNames.GET_STAKING_INFO_FAIL
                    })
                })
        }))
        .catch((err: any) =>
            Observable.of({
                type: ActionNames.GET_STAKING_INFO_FAIL
            }))
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49247001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档