locked
Problem when I read a txt. file RRS feed

  • Question

  • Hi everybody !

    I'm have a problem (again) for reading a txt file.

    this is my code:

            fn_loadInfo(2);
    
        function fn_loadInfo( position ) {
    
            var path = "data\\info.txt";
            var newArray;
    
            Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(path)
            .then(function (file) {
                // (1)
                console.log(1);
                return Windows.Storage.FileIO.readTextAsync(file);
            }).then(function (myText) {
                // (2)
                console.log(2);
                newArray = myText.split('|');
                return newArray[position];
            });
            // (3)
            console.log(3);
        }

    I have to return a value of my array (who contains my file content) .. but if I run my app, run (1) (3) then (2) , so , nothing is return to me !!

    I need a light! =/

    Wednesday, May 9, 2012 7:46 PM

Answers

  • As Mr. Hays said, because your function calls async functions, you can't just return a value as you would for a synchronous function.  It is possible, however, to encapsulate this task into your own async function like this example which calls your new async function and when finished, outputs the result to the console:

    (function () {
        "use strict";
    
        //Click event handler
        function getResult() {
            
            //Call the function and output result to the console.
            asyncLoadInfo(2)
            .then(function (result) {
                console.log(result);
            });
        }
    
        //Async function to fetch data from a text file by position
        function asyncLoadInfo(position) {
            return new WinJS.Promise(function (complete) {
                var path = "data\\info.txt";
                var newArray;
                Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(path)
                .then(function (file) {
                    return Windows.Storage.FileIO.readTextAsync(file);
                }).then(function (myText) {
                    newArray = myText.split('|');
                    complete(newArray[position]);
                });
            });
        }
    
        //Hook up events
        WinJS.Utilities.ready(function () {
            document.getElementById('doit').addEventListener('click', getResult, false);
        });
    
    })();

    Ideally, you would want to augment your new async function with error processing as well.




    Wednesday, May 9, 2012 9:41 PM
  • You should pass in as a function what you want to happen after the results finish reading, that way it remains async and you can update your UI.
    Thursday, May 10, 2012 8:15 PM

All replies

  • If you are trying to return a value from fn_loadInfo then I don't think your design is going to work. You are using async methods and expecting a sync response. If you need to use the result of the .then function you should pass in a callback that gets executed in a .then with the result you return from the 2nd .then.
    Wednesday, May 9, 2012 8:39 PM
  • As Mr. Hays said, because your function calls async functions, you can't just return a value as you would for a synchronous function.  It is possible, however, to encapsulate this task into your own async function like this example which calls your new async function and when finished, outputs the result to the console:

    (function () {
        "use strict";
    
        //Click event handler
        function getResult() {
            
            //Call the function and output result to the console.
            asyncLoadInfo(2)
            .then(function (result) {
                console.log(result);
            });
        }
    
        //Async function to fetch data from a text file by position
        function asyncLoadInfo(position) {
            return new WinJS.Promise(function (complete) {
                var path = "data\\info.txt";
                var newArray;
                Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(path)
                .then(function (file) {
                    return Windows.Storage.FileIO.readTextAsync(file);
                }).then(function (myText) {
                    newArray = myText.split('|');
                    complete(newArray[position]);
                });
            });
        }
    
        //Hook up events
        WinJS.Utilities.ready(function () {
            document.getElementById('doit').addEventListener('click', getResult, false);
        });
    
    })();

    Ideally, you would want to augment your new async function with error processing as well.




    Wednesday, May 9, 2012 9:41 PM
  • thx ! it works for show the results on screen... but I need return the result ...

    how can I read a file and get/return your content using a synchronous function ! ? 

    Thursday, May 10, 2012 8:01 PM
  • You should pass in as a function what you want to happen after the results finish reading, that way it remains async and you can update your UI.
    Thursday, May 10, 2012 8:15 PM