7

I am using the react-native-fs module, however my app seems to be freezing on it. I have the following piece of code in one of my React-Native methods:

console.log("One")
await RNFS.downloadFile({
    fromUrl: 'http://example.com/data/blah.zip',
    toFile: RNFS.DocumentDirectoryPath + "/userdata/blah.zip",
}).promise
console.log("Two")

"One" is echoed to the console, but "Two" never gets echoed. There is no error thrown. The URL does in fact exist and the RNFS.DocumentDirectoryPath + /userdata directory does exist.

What am I doing wrong? Why is no error thrown? Why is it hanging at this point?

Update

Unfortunately, because it only seems to happen sometimes it is hard to diagnose or to produce a smaller test. What I've had to do is when this happens I have to shut down the react-native command prompt node window and rerun react-native run-android. Once I do that it proceeds perfectly.

kojow7
  • 10,308
  • 17
  • 80
  • 135
  • Do you need a .then clause after promise? RNFS.downloadFile({fromUrl:url, toFile: path}).promise.then(res => { console.log('1.5') }); – Dan Dec 26 '17 at 20:51
  • @Dan, I am using await instead. But even if I try a .then instead of await it still hangs at the same spot. – kojow7 Dec 26 '17 at 21:15
  • Hmmm, for whatever reason this error seems to be intermittent. Sometimes it seems to work and other times it doesn't seem to work. – kojow7 Dec 26 '17 at 22:04
  • Could the issue be from the server providing the .zip? Maybe this can be narrowed downed further if you try downloading only from a url that you are 100% sure to be reliable. Also, how often is this code being ran? Would there be issue of writing to the same file path at the same time? – Dan Dec 26 '17 at 22:45
  • As far as I know, the server should be stable as it is working fine for everything else and I can access the file just fine through a browser. Even so, shouldn't an error be getting thrown if there was a server error? – kojow7 Dec 26 '17 at 22:48
  • You might need to catch the error. Try adding .then(...).catch( error => { console.log('ERROR: ', err) }) – Dan Dec 26 '17 at 23:05
  • Are you testing on a device or the emulators? How is the internet connection? There is an issue currently where the promise never resolves if the connection messes up. https://github.com/itinance/react-native-fs/issues/239 – Joshua Leonard Dec 27 '17 at 03:10
  • @JoshuaLeonard I am testing on an Android device through USB cable and using adb over wifi. My Internet connection does seem to work fine for all other purposes, so not sure why that would be the problem. But the link you posted does seem to have similar symptoms so maybe there is some related issue. – kojow7 Dec 27 '17 at 04:08
  • Hey! Did you solve it? Maybe it's library problem? – Kesha Antonov Feb 23 '19 at 17:35
  • @KeshaAntonov No, never did solve it. If you read my Update, I did find a work around, but not a very convenient. – kojow7 Feb 23 '19 at 18:26
  • @kojow7 I'll try to debug java code to see what's happening – Kesha Antonov Feb 24 '19 at 04:41

1 Answers1

0

I think it may be erroring silently. Try wrapping the whole block in a try/catch.

try {
  await RNFS.downloadFile({
    fromUrl: 'http://example.com/data/blah.zip',
    toFile: RNFS.DocumentDirectoryPath + "/userdata/blah.zip",
  }).promise
} catch (error) {
  console.log(error);
}
Chris Geirman
  • 9,474
  • 5
  • 37
  • 70