locked
readTextAsync

    Question

  •    Sonehow this code is not working. It never enters the 'Windows.Storage.FileIO.readTextAsync' block.

    Is there a problem using MultipleFiles ???

    The code:

             openPicker.fileTypeFilter.replaceAll([".txt"]);
                openPicker.pickMultipleFilesAsync().then(function (files) {
                    if (files.size > 0) {
                        for (var i = 0; i < files.size; i++) {
                            outputName += files[i].name;
                            Windows.Storage.FileIO.readTextAsync(files[i]).done(function (content) {
                                fileContent += content;               // will never enter this code. It blocks out the whole block
                                }
                        )}
                        var moves = onBufScan(fileContent);

     

    Sunday, July 21, 2013 3:55 AM

Answers

  • There's not a problem with multiple files, no. What you're seeing here is instead the result of async API behavior. When you iterate the files collection and call readTextAsync for both, the multiple callbacks to your anonymous completed handler will come later, and in any order. This means that your loop will complete potentially before any callbacks happen, so your call to onBufScan may be called with either an empty fileContent variable or one that's only partially populated.

    There are ways to handle these sorts of things sequentially by joining the promises that come back from readTextAsync. If you don't care about the order of fulfillment of those async calls, you can iterate the files collection, call readTextAsync, push the promise each call into an array, then pass that array to WinJS.Promise.join. You'd then attach a single completed handler to that joined promise, in which you'll get an array of the individual results that you can then process into your fileContent variable.

    If you need sequential results in the same order as files, that gets a little more complicated, but I have that example in a post on the Windows Developer Blog: http://blogs.msdn.com/b/windowsappdev/archive/2013/06/11/all-about-promises-for-windows-store-apps-written-in-javascript.aspx. That post is good about promises in general too, and you should find it helpful to understand async handling more deeply. An expanded version of that post is in Appendix A of the second edition preview linked below.

    Kraig

    Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press

    Also see second edition preview


    Monday, July 22, 2013 2:58 PM
  • Thank you for the reply. I am still studying it. I expected something like promise played a role.

    In the mean time  I found another approach, and will come back to this later.

    Thanks

    Wednesday, July 24, 2013 12:56 AM

All replies

  • There's not a problem with multiple files, no. What you're seeing here is instead the result of async API behavior. When you iterate the files collection and call readTextAsync for both, the multiple callbacks to your anonymous completed handler will come later, and in any order. This means that your loop will complete potentially before any callbacks happen, so your call to onBufScan may be called with either an empty fileContent variable or one that's only partially populated.

    There are ways to handle these sorts of things sequentially by joining the promises that come back from readTextAsync. If you don't care about the order of fulfillment of those async calls, you can iterate the files collection, call readTextAsync, push the promise each call into an array, then pass that array to WinJS.Promise.join. You'd then attach a single completed handler to that joined promise, in which you'll get an array of the individual results that you can then process into your fileContent variable.

    If you need sequential results in the same order as files, that gets a little more complicated, but I have that example in a post on the Windows Developer Blog: http://blogs.msdn.com/b/windowsappdev/archive/2013/06/11/all-about-promises-for-windows-store-apps-written-in-javascript.aspx. That post is good about promises in general too, and you should find it helpful to understand async handling more deeply. An expanded version of that post is in Appendix A of the second edition preview linked below.

    Kraig

    Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press

    Also see second edition preview


    Monday, July 22, 2013 2:58 PM
  • Thank you for the reply. I am still studying it. I expected something like promise played a role.

    In the mean time  I found another approach, and will come back to this later.

    Thanks

    Wednesday, July 24, 2013 12:56 AM