typescript -
问题描述
解决方案
- 如果您将函数声明为
async
不需要使用then
. - 你返回的东西
then
无处可去,因为你不存储也不返回它getSymbolStats
只需使用await
并存储结果或添加return
之前request.then
。
with await
(更直接的代码,我更喜欢这种方式)。
const res: AxiosResponse<ISymbolStats> = await axios.get<ISymbolStats>(`${api.baseURL}${api.symbolStats}`, {params: {symbol}});
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;
添加return
:
let request: Promise<AxiosResponse> = axios.get(`${api.baseURL}${api.symbolStats}`, {params: {symbol}});
return 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;
})
推荐阅读
- android - 如何从协程返回响应值
- javascript - 为什么不调用订阅
- flutter - Flutter:如何减小 ModalBottomSheet 中 CupertinoPicker 的大小?
- c# - Microsoft Teams 机器人向团队的每个成员发送主动消息
- python - 为什么打印目录中的两个文件名,而列表中仅扩展一个文件的内容?
- reactjs - React 应用程序中嵌入了多个 power bi 报告
- java - Java“令牌“;”上的语法错误,{此令牌后预期”错误
- php - Foreach 与 php 中的第一个数组不同
- node.js - Typedoc:仅记录特定文件导出的内容
- swift - 尝试安装新 pod 后在框架文件中出现错误