【问题标题】:returning data from then() returns Promise<void> instead of Promise<data>从 then() 返回数据返回 Promise<void> 而不是 Promise<data>
【发布时间】: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


【解决方案1】:
  1. 如果您将函数声明为async,则无需使用 then
  2. 你从then返回的东西无处可去,因为你不存储也不从getSymbolStats返回它

只需使用await 并存储结果或在request.then 之前添加return

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

【讨论】:

    猜你喜欢
    • 2017-10-31
    • 2018-11-03
    • 1970-01-01
    • 2021-02-06
    • 2017-03-17
    • 2018-04-07
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多