locked
Promises occuring out of order

    Question

  • I have the following chain of promises:

    var ws = Windows.Storage;
    
    Debug.enableFirstChanceException(true);
    var myDataSource = {};
    
    document.addEventListener("DOMContentLoaded", function (e) {
    Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("taskList.todo", Windows.Storage.CreationCollisionOption.openIfExists)
    .then(function (file) {
        return file.openAsync(Windows.Storage.FileAccessMode.readWrite);
    })
    .then(function (stream) {
        var inputStream = stream.getInputStreamAt(0);
        var reader = new Windows.Storage.Streams.DataReader(inputStream);
    
        var size = stream.size;
        if (size > 0) {
            reader.loadAsync(size).then(function () {
                var fileContents = reader.readString(size);
                var taskArray = fileContents.split(",", 1000);
                myDataSource = new WinJS.UI.ArrayDataSource(taskArray);
            });
        }
    })
    .then(function () {
        WinJS.UI.processAll()
                .then(function () {
    
            var basicListView = WinJS.UI.getControl(
                        document.getElementById("taskChecklist"));
            basicListView.dataSource = myDataSource;
        });
    });
    
    


    When I run the app, the WinJS.UI.ProcessAll executes, AND THEN the File Read (above it in the chain) executes.  I am probably missing something, but I thought that the .then nomenclature told the runtime to wait until the code within is complete.  What have I done wrong?


    Check out my new C# 2010 All In One for Dummies book at Amazon!
    Friday, December 23, 2011 3:35 AM

Answers