Search code examples
javascripthtmldom-eventswindows-store-appswinjs

How to return array from JavaScript function that retrieves data from text file?


I am building a Windows 8 Store app with HTML/CSS/JavaScript. I am reading in data from a text file through a function, and then putting that data into an array. I am trying to return the array through the function, but it is not working. Any help would be greatly appreciated. I've attached my code snippet.

// Load user data
var DefineUserData = function LoadUserData() {
return Windows.Storage.ApplicationData.current.localFolder.getFileAsync(loadfile).done(function (UserFile) {
   return Windows.Storage.FileIO.readTextAsync(UserFile).done(function (fileResult) {

       var userdata = new Object();
       var dataobject = {};
       var innercount;
       var outercount;
       var fileResultByLines = fileResult.split("\n");

       for (outercount = 0; outercount <= (fileResultByLines.length - 2) ; outercount++) {
           var tempArray = fileResultByLines[outercount].split(",");
           dataobject.metrictitle = tempArray[0];
           dataobject.numinputs = tempArray[1];
           dataobject.inputs = new Array();
           for (innercount = 0; innercount <= parseInt(dataobject.numinputs) ; innercount++) {
               dataobject.inputs[innercount] = tempArray[innercount + 2];
            }
           userdata[outercount] = dataobject;
        }               
         return userdata;
      });
   },
    function (errorResult) {
       document.getElementById("resbutton1").innerText = errorResult;
    })
}

Solution

  • Your DefineUserData function is returning a Promise, not a value. Additionally done functions don't return anything. Instead you'll need to use then functions instead of done functions in DefineUserData and then handle add a done function (or then) to the code that calls this function.

    Also, You can make your promises easier to read, and easier to work with by chaining then functions instead of nesting them.

    Currently on Win7 at the office so I can't test this, but try something similar to this pseudo-code. Note then functions instead of done. The last then returns your data. Sample snippet afterwards to illustrate calling this and handling the result.

    // modified version of yours
    var DefineUserData = function LoadUserData() {
        return Windows.Storage.ApplicationData.current.localFolder
            .getFileAsync(loadfile)
            .then(function (UserFile) {
                return Windows.Storage.FileIO.readTextAsync(UserFile);
            }).then(function (fileResult) {
    
                var userdata = new Object();
                var dataobject = {};
                var innercount;
                var outercount;
                var fileResultByLines = fileResult.split("\n");
    
                for (outercount = 0; outercount <= (fileResultByLines.length - 2) ; outercount++) {
                    var tempArray = fileResultByLines[outercount].split(",");
                    dataobject.metrictitle = tempArray[0];
                    dataobject.numinputs = tempArray[1];
                    dataobject.inputs = new Array();
                    for (innercount = 0; innercount <= parseInt(dataobject.numinputs) ; innercount++) {
                        dataobject.inputs[innercount] = tempArray[innercount + 2];
                    }
                    userdata[outercount] = dataobject;
                }               
    
                return userdata;
            },
            function (errorResult) {
                document.getElementById("resbutton1").innerText = errorResult;
            });
    }
    
    
    // some other code...
    
    DefineUserData.done(function (userdata) {
        // do something
    });