1

I'm still quite new to Promises and would like some clarification.

For some context, I'm trying to get a file from an URL. The file is stored using IPFS and when there's a timeout I would like to try and call axios.get() again.

My current problem is, that it looks like the file is being downloaded but the function rarely returns anything, it looks like the download is super slow, however if I open the link directly I get the file instantly, making me think there's something wrong with my promises, even though no then() nor catch() is ran. My current code is as follows:

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)
}
No Sarcasm
  • 35
  • 5

2 Answers2

0

There is a plug-in for axios called axios-retry which you can use:

import axiosRetry from 'axios-retry';

axiosRetry(axios, { retries: 1 });

axios.get(url); // It will retry one time if the request fail.
NeNaD
  • 18,172
  • 8
  • 47
  • 89
0

Additional to the axios-retry plugin, it would be helpful if you increase the axios default timeout, in case it is not enough to read the file.

instance.get('/yourRequestedURL', {
  timeout: 15000 //Try to give a little more than the timeout of the file
});
Alvin
  • 762
  • 4
  • 14