多个 Axios return 无法 return 该值,但在控制台中它即将到来

Mutiple Axios return not able to return the value but in cosole it is coming

多个 Axios return 无法 return 该值,但在控制台中它即将到来 第一个函数能够 return 数据> 第二个和第三个做不到>

<第一个函数能够return数据> <第二个和第三个做不到>

async getdata() {
  let url = `xxxxxxxx`;
  this.axiosConfig = {
    withCredentials: false,
    maxRedirects: 3
  };
  let config: AxiosRequestConfig = { ...this.axiosConfig
  };
  //axios.defaults.headers.common = { 'Authorization': 'Bearer ' + this.token }
  var result = await axios.get(url, config);
  console.log(result.data); // --------------> data is coming here
  return result.data; //------------>able to return the data from here
}
async getfame() {
  var res = await this.getdata()
  res.map(async(item: any) => {
    let url = `xxxxxxxx`;
    this.axiosConfig = {
      withCredentials: false,
      maxRedirects: 3
    };
    let config: AxiosRequestConfig = { ...this.axiosConfig
    };
    //axios.defaults.headers.common = { 'Authorization': 'Bearer ' + this.token }
    var result1 = await axios.get(url, config);
    console.log(result1.data); // -----------------> data coming here
    return result1.data; //------------> not able to return the data from here
    //return axios.get<string>(url, config);
  })
}
async getfix() {
  var res1 = await this.getdata()
  res1.map(async(item: any) => {
    let url = `xxxxxxxx`;
    this.axiosConfig = {
      withCredentials: false,
      maxRedirects: 3
    };
    let config: AxiosRequestConfig = { ...this.axiosConfig
    };
    //axios.defaults.headers.common = { 'Authorization': 'Bearer ' + this.token }
    var result2 = await axios.get(url, config);
    console.log(result2.data); // -----------------> data coming here
    return result2.data; //------------> not able to return the data from here
    //return axios.get<string>(url, config);   //-------------> tried this way but still not coming
  })
}

在你的第二个和第三个函数中,你 return 在数组映射中调用那些 axios 调用。外部函数没有 returning 任何东西。

如果您在每次 .map() 调用之前添加一个 return,那么这些函数将 return 您预期结果的数组。

async getfame() {
  var res = await this.getdata()
  return res.map(async(item: any) => {
    /* ... */
    return result1.data; 
  })
}
async getfix() {
  var res1 = await this.getdata()
  return res1.map(async(item: any) => {
    /* ... */
    return result2.data;
  })
}