Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
43
rated 0 times [  44] [ 1]  / answers: 1 / hits: 69897  / 5 Years ago, thu, february 21, 2019, 12:00:00

Function lacks ending return statement and return type does not include 'undefined'.




In the following async await function I had return type of Promise: <any> but I wanted to correct that so I did the following:



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);
}
}


However that creates the error above.



enter



Where getMarkets is called:



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));
});
}


What is/are the proper Types for that Promise<> syntax?






I also tried this which I expected to be the correct way, but got a missing return for Promise, but this is an async await function which is why the return is in the try statement:



export const getMarkets = async (): Promise<IGetMarketsRes> => {



enter


More From » reactjs

 Answers
0

The best solution would be to throw an error on the catch, instead of undefining the return type. The undefined type removes the whole point of using TypeScript otherwise.


Try this:


export const getMarkets = async (): Promise<IGetMarketsRes> => {
try {
// Your code :)
} catch (err) {
// Throw error
throw(err)
}
}

[#52557] Saturday, February 16, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zainsagez

Total Points: 555
Total Questions: 99
Total Answers: 96

Location: Honduras
Member since Sat, Jul 24, 2021
3 Years ago
;