Answered How is WinJS.Promise.timeout supposed to be used?

  • Thursday, March 08, 2012 2:05 PM
     
     

    I am currently using

    myAsync()

    return new WinJS.Promise(function(complete, error, progress) {

      return setTimeout(function() {

         /// work

        complete();

     }

    }

    How could I convert this to the timout promise that uses setImmediate?

All Replies

  • Thursday, March 08, 2012 2:50 PM
    Moderator
     
     Answered

    Hi Phil,

    This is documented here:

    http://msdn.microsoft.com/en-us/library/windows/apps/br229729.aspx

    timeout

    Type: Int

    The timeout period in milliseconds. If this value is zero or not specified                 msSetImmediate is called, otherwise setTimeout is called.

    Perhaps I don't understand the question?

    -Jeff


    Jeff Sanders (MSFT)

  • Thursday, March 08, 2012 4:10 PM
     
     

    I read the docs, but I do not understand how to use the function: How would my code above needed to be refactored for WinJS.Promise.timeout?

    myAsync()

    return WinJS.Promise.timeout(0, function(complete, error, progress) {

         /// work

        complete();

    }

    But this throws an exception, cause complete is not defined

  • Thursday, March 08, 2012 4:52 PM
     
     Answered Has Code

    Hi Phil,

    Typically you use timeout when you want a small delay before something happens. So let's say you wanted to wait 5 seconds before taking some action:

    WinJS.Promise.timeout(5000).then(function(c) { 
      console.log("Roughly five seconds later...");
    });

    Of course this could be chained off another promise:

    DoSomethingAsync().then(WinJS.Promise.timeout(5000)).then(function(c) { 
      console.log("Roughly five seconds after DoSomething returned...");
    });

    The second parameter is a little bit different. That's if you wanted to cancel something if it doesn't return fast enough. So, say you wanted to cancel an XHR if it didn't return in 250ms:

    WinJS.timeout(250, WinJS.xhr(options).then(updateUI));

    Cheers,

    -Jeff

  • Monday, March 12, 2012 7:21 AM
     
      Has Code

    WinJS.Promise.timeout(5000).then(function(c) { console.log("Roughly five seconds later..."); });

    This could also use the timeout(0) for msImmediate?

  • Monday, March 12, 2012 4:10 PM
     
     

    Yep, if you specify a length of 0, WinJS will call msSetImmediate instead of setTimeout internally.

    Cheers,

    -Jeff