我正在尝试定义一个承诺的解决方案的类型。以下是代码的一部分,或者如果你想在github上查找它:https://github.com/Electra-project/Electra-Desktop/blob/master/src/app/header/epics.ts
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) => {。然而,我不确定解决的类型。
有谁能指导我这里应该是什么类型的解决方案?
发布于 2018-03-13 17:51:08
您可以像这样定义您自己的类型,例如:
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
}))
}https://stackoverflow.com/questions/49247001
复制相似问题