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.
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> => {
Advertisement
Answer
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) } }