locked
WinJS.Promise Not Acting as I Expect RRS feed

  • Question

  • I am working in a Windows 8 JavaScript app and a WinJS.Promise is not working as I'd expect. Here's my async method...
    function MyFiveSecondFunctionAsync() {
       
    return new WinJS.Promise(function (c, e, p) {
           
    var start = new Date().getTime();
           
    var delay = 5000;
           
    while (new Date().getTime() < start + delay) p();
            c
    ();
       
    });
    }

    And here's what I'm calling it with...

    document.querySelector("#status").innerText = "working";
    MyFiveSecondFunctionAsync().done(function () {
        document
    .querySelector("#status").innerText = "done";
    });
    document
    .querySelector("#status").innerText = "free";

    This is in a button click. I would expect for my status div to very briefly say "working" after I click the button, but then say "free" for 5 seconds and finally say "done" when the 5 second delay is complete. Instead, the UI thread is blocking for 5 seconds and then the status div says "free".

    I thought that execution would return to the line that sets status to "free", but debugging shows that it hits the promise function before that. So the status is set to "done" after 5 seconds and then set to "free". In other words, my asynchronous function is just simply not acting asynchronously.

    Wednesday, June 27, 2012 1:27 AM

Answers