Function lacks ending return statement and return type does not include 'undefined'.
在以下异步等待函数中,我的返回类型为 Promise: <any>
但我想纠正它,所以我做了以下事情:
export const getMarkets = async (): Promise<IGetMarketsRes> => {
try {
const nomicsUSD = prepHeaders('USD');
const marketUSD = await nomicsUSD.get(exchangeMarketPrices);
const nomicsUSDC = prepHeaders('USDC');
const marketUSDC = await nomicsUSDC.get(exchangeMarketPrices);
const nomicsUSDT = prepHeaders('USDT');
const marketUSDT = await nomicsUSDT.get(exchangeMarketPrices);
console.log('marketUSD', marketUSD);
return {
marketUSD: marketUSD.data,
marketUSDC: marketUSDC.data,
marketUSDT: marketUSDT.data
}
} catch (err) {
console.error(err);
}
}
然而,这会产生上述错误。
在哪里getMarkets
被称为:
export const fetchMarketPrices = (asset: string) => (dispatch: any) => {
dispatch(actionGetMarketPrices);
return getMarkets().then((res) => {
const { marketUSD, marketUSDC, marketUSDT } = res;
const combinedExchanges = marketUSD.concat(marketUSDC).concat(marketUSDT);
const exchangesForAsset = combinedExchanges.filter((marketAsset: IMarketAsset) =>
marketAsset.base === asset);
return dispatch(actionSetMarketPrices(exchangesForAsset));
});
}
什么是合适的类型 Promise<>
语法?
我也试过这个,我希望这是正确的方法,但是缺少 Promise 的返回,但这是一个异步等待函数,这就是返回在 try
中的原因。声明:
export const getMarkets = async (): Promise<IGetMarketsRes> => {
最佳答案
最好的解决方案是在捕获时抛出错误,而不是取消定义返回类型。 undefined
类型消除了使用 TypeScript 的全部意义。
试试这个:
export const getMarkets = async (): Promise<IGetMarketsRes> => {
try {
// Your code :)
} catch (err) {
// Throw error
throw(err)
}
}
关于javascript - 函数缺少结束返回语句且返回类型不包括 'undefined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54812453/