axios 返回承诺而不是数据

Jga*_*nie 0 reactjs axios

我正在使用 axios 从 IPFS 查询一些数据,问题是调用特定 api 后返回值是来自 axios 的承诺。

const getNFTDetail = async (url: string) => {
    const urlIPF = url.replace("ipfs://", "https://cloudflare-ipfs.com/ipfs/");
    try {
      return await axios.get(urlIPF).then((res) => {
        return res.data;
      });
    } catch (error) {
      console.log(error);
    }
  };
Run Code Online (Sandbox Code Playgroud)

我得到的回应:

在此输入图像描述

有没有办法等到承诺得到解决?,正如你所看到的,我已经在函数调用上使用了 async wait 。

Ash*_*hma 5

只是,决定是否使用async / wait.then / .catch

const getNFTDetail = async (url: any) => {
  const urlIPF = url.replace("ipfs://", "https://cloudflare-ipfs.com/ipfs/");
  const { data } = await axios.get(urlIPF);
    return data;
};
Run Code Online (Sandbox Code Playgroud)

或者

const getNFTDetail = (url: any) => {
  const urlIPF = url.replace("ipfs://", "https://cloudflare-ipfs.com/ipfs/");

  axios.get(urlIPF).then(({data}) => {
    // use your data here
  }).catch((err)=>{
    console.log(err);
  };
};
Run Code Online (Sandbox Code Playgroud)