我正在使用 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 。
只是,决定是否使用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)
| 归档时间: |
|
| 查看次数: |
4565 次 |
| 最近记录: |