【发布时间】:2021-03-17 09:29:25
【问题描述】:
我从 API 获取数据如下:
export const getSymbolStats = async (symbol: string) => {
let requiredData: IRequiredSymbolStats;
let request: Promise<AxiosResponse> = axios.get(`${api.baseURL}${api.symbolStats}`, {params: {symbol}});
request.then((res: AxiosResponse<ISymbolStats>) => {
const symbolStats: ISymbolStats = res.data;
// Destructure symbol stats.
const highPrice: string = symbolStats.highPrice;
const lowPrice: string = symbolStats.lowPrice;
const priceChangePercent: string = symbolStats.priceChangePercent;
const volume: string = symbolStats.volume;
const weightedAvgPrice: string = symbolStats.weightedAvgPrice;
// return an object with the required data.
requiredData = {
symbol,
highPrice,
lowPrice,
priceChangePercent,
volume,
weightedAvgPrice,
};
return requiredData;
})
}
并在此函数中检索数据:
export const getStats = async (msg: Message, match: RegExpExecArray | null): Promise<void> => {
const id = msg.chat.id;
const symbol: OrUndefined<string> = validateSymbol(id, match);
if (!symbol) return;
// make API call to get stats for symbol.
let data = await getSymbolStats(symbol);
};
第二个函数中的data 显示void,而不是我在then() 块中返回的IRequiredSymbolStats 类型。这是为什么呢?
【问题讨论】:
-
你为什么在
async function中使用then?
标签: typescript promise async-await es6-promise