locked
Reading several files asynchronous problem

    Question

  • An app used single page navigation need to read several files in default.js before navigating to home page.

    These files organize as follow:

    Documents Libarary

        └ app_dir

                ├file1

                └ sub_dir

                       ├file2

                       └ file3

    My code reading these files could be:

    Windows.Storage.KnownFolders.documentsLibrary.getFolderAsync("app_dir").then(function(fol){
        fol.getFileAsync("file1").then(function(file){
            do_something1();
        });
        fol.getFolderAsync("sub_dir").then(function(fol){
            fol.getFilesAsync().then(function(files){
                do_something2();
            });
        });
    });
    WinJS.Navigation.navigate should be invoked after do_something1() and do_something2() executed. Where should I invoke navigate? Or is there a better approach? Thanks a lot.
     
    Wednesday, November 20, 2013 8:59 AM

Answers

  • Yeah, there is an error handler for the promise.then(...) which you can use if the promise error's out. For example, using the same approach as above - notice the bold section which has the error handler:

                ......
                ......
                }).then(function (file) {
                    do_something1();
                    return parentFolder.getFolderAsync("sub_dir");
                }, function (error) {   // add an Error handler if the file does not exist
                    console.log("file does not exist");
                    return parentFolder.getFolderAsync("sub_dir"); // read the sub-dir if the files do not exist
                }).then(function (childFolder) {
                ......            
                ......
    


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Thursday, November 21, 2013 1:16 AM
    Moderator

All replies

  • You can use an approach called "Chaining promises" and use code that looks like the below. Btw, is there any reason you are using the documentsLibrary capability? See this blog for more info: http://blogs.msdn.com/b/wsdevsol/archive/2013/05/09/dealing-with-documents-how-not-to-use-the-documentslibrary-capability-in-windows-store-apps.aspx

    That's why I have replaced your documentsLibrary with picturesLibrary.

        WinJS.UI.Pages.define("/pages/home/home.html", {
            // This function is called whenever a user navigates to this page. It
            // populates the page elements with the app's data.
            ready: function (element, options) {
                // TODO: Initialize the page here.
                document.getElementById("btnEnlist").addEventListener("click", this.btnEnlist_Clicked, false);
            },
            btnEnlist_Clicked: function () {
                var parentFolder = null;
                Windows.Storage.KnownFolders.picturesLibrary.getFolderAsync("app_dir").then(function (fol) {
                    parentFolder = fol;
                    return fol.getFileAsync("file1.txt");
                }).then(function (file) {
                    do_something1();
                    return parentFolder.getFolderAsync("sub_dir");
                }).then(function (childFolder) {
                    return childFolder.getFilesAsync();
                }).then(function (files) {
                    do_something2();
                });
            }
        });
    
        function do_something1() {
            console.log("do_something1 called");
        }
        function do_something2() {
            console.log("do_something2 called");
        }


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Wednesday, November 20, 2013 6:59 PM
    Moderator
  • Thank you for your reply. It really works.

    But I have another question. If my files organize like below:

    Picture Libarary

        └ app_dir

                ├file1

                └ sub_dir

                       ├file2

                       └ file3

    By using your approach, sub_dir and the other 2 files will never be read when trying to read file1 which is not exist, . Do you have some advice to improve this?


    Thursday, November 21, 2013 1:07 AM
  • Yeah, there is an error handler for the promise.then(...) which you can use if the promise error's out. For example, using the same approach as above - notice the bold section which has the error handler:

                ......
                ......
                }).then(function (file) {
                    do_something1();
                    return parentFolder.getFolderAsync("sub_dir");
                }, function (error) {   // add an Error handler if the file does not exist
                    console.log("file does not exist");
                    return parentFolder.getFolderAsync("sub_dir"); // read the sub-dir if the files do not exist
                }).then(function (childFolder) {
                ......            
                ......
    


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Thursday, November 21, 2013 1:16 AM
    Moderator