views:

87

answers:

2

Hi everybody. I try to read some file with google's GDownloadUrl and it works only from time to time.

  • failure means fileRows == "blah blah"
  • success means fileRows == (real file content)

I've noticed, however, that when I cease (with Firebug) the execution on line 3 for a couple of seconds, it succeeds more often. Maybe it is some kind of threading bug, then? Do You guys have any tip or idea?

1 var fileContent = "blah blah";
2 availabilityFile = "input/available/" + date + ".csv";
3 GDownloadUrl(availabilityFile, function(fileData) {
4     fileContent = fileData;
5 });
6 fileRows = fileContent.split("\n");
+3  A: 

GDownloadUrl is an asynchronous operation. So, Line 6 is executed immediately, without waiting for GDownloadUrl to finish.

Use the onload function to do the things that can only be done after the download is complete.

chris
A: 

Thanks for the explanation. So it seems, that such a code should word as expected.

1 var fileContent = "blah blah";
2 availabilityFile = "input/available/" + date + ".csv";
3 GDownloadUrl(availabilityFile, function(fileData) {
4     fileRows = fileData.split("\n");
5 }); 
Michał Pękała