Table of Contents
Issue
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> => {
Solution
Solution to keep the try catch
export const getMarkets = async (): Promise<IGetMarketsRes | undefined> => {
try {
const nomicsUSD = prepHeaders('111');
const marketUSD = await nomicsUSD.get(exchangeMarketPrices);
const nomicsUSDC = prepHeaders('222');
const marketUSDC = await nomicsUSDC.get(exchangeMarketPrices);
const nomicsUSDT = prepHeaders('333');
const marketUSDT = await nomicsUSDT.get(exchangeMarketPrices);
const { data: dataUSD } = marketUSD;
const { data: dataUSDC } = marketUSDC;
const { data: dataUSDT } = marketUSDT;
if (R.isEmpty(dataUSD) || R.isEmpty(dataUSDC) || R.isEmpty(dataUSDT)) {
console.error('Market data unavailable');
}
return {
marketUSD: marketUSD.data,
marketUSDC: marketUSDC.data,
marketUSDT: marketUSDT.data
}
} catch (error) {
console.error(error);
}
}
A Much better DRY example
export const fetchMarket = async (currency: string): Promise<any> => {
try {
const request = prepHeaders(currency);
const response = await request.get(EXCHANGE_MARKET_PRICES);
if (!response) {
throw new Error('USD Markets unavailable.');
}
return response.data;
}
catch(err) {
console.error(err);
}
}
// GET Market prices
// http://docs.nomics.com/#operation/getMarkets
export const getMarkets = async (): Promise<IGetMarketsRes | undefined> => {
try {
const markets: IMarketRes = {};
for (let currency of BASE_CURRENCIES) {
const key = 'market' + currency;
markets[key] = await fetchMarket(currency);
}
return {
marketUSD: markets['marketUSD'],
marketUSDC: markets['marketUSDC'],
marketUSDT: markets['marketUSDT'],
}
} catch (error) {
console.error(error);
}
}
And the call from the actions file:
// Fetch USD, USDC & USDT markets to filter out Exchange List.
export const fetchMarketPrices = (asset: string) => (dispatch: any) => {
dispatch(actionGetMarketPrices);
return getMarkets().then((res) => {
if (res) {
const exchangesForAsset = combineExchangeData(asset, res);
return dispatch(actionSetMarketPrices(exchangesForAsset));
}
});
}
Answered By – Leon Gaban
Answer Checked By – Marilyn (BugsFixing Volunteer)