我对承诺还是很陌生的,我希望得到一些澄清。
对于某些上下文,我试图从URL中获取一个文件。该文件使用IPFS存储,当出现超时时,我想再次尝试调用axios.get()
。
我当前的问题是,它看起来像是正在下载文件,但是函数很少返回任何内容,看起来下载速度非常慢,但是如果我直接打开链接,我就会立即得到文件,这让我觉得我的承诺有问题,即使没有运行then()
或catch()
。我的当前代码如下:
const getIPFSMedia = (url) => {
return axios.get(url, { responseType: 'blob' }).then((response) => {
return URL.createObjectURL(response.data);
}).catch((e) => {
if (e.message === GET_IPFS_TIMED_OUT) {
return getIPFSMedia(url);
} else {
console.log(e.message);
}
});
}
const fetchData = async() => {
const image = await getIPFSMedia(url); // Slow or rarely returns, but when it does it has my file in it
console.log(image)
}
发布于 2022-09-22 22:44:35
有一个名为axios-重试的axios插件,您可以使用:
import axiosRetry from 'axios-retry';
axiosRetry(axios, { retries: 1 });
axios.get(url); // It will retry one time if the request fail.
发布于 2022-09-22 23:12:47
除了axios重试插件之外,如果您增加了axios默认超时,以防止只读取文件是不够的,这将是有帮助的。
instance.get('/yourRequestedURL', {
timeout: 15000 //Try to give a little more than the timeout of the file
});
https://stackoverflow.com/questions/73821307
复制相似问题