Canceling Promises

It is useful to have the ability to stop long running functions. For this reason, some promises support cancellation. Some promises, but not all, support cancellation. When you cancel a promise, the promise’s error callback is executed.

For example, the code in Listing 2.12 uses the WinJS.xhr() method to perform an Ajax request. However, immediately after the Ajax request is made, the request is canceled.

LISTING 2.12 Canceling a Promise (promiseCancel\promiseCancel.html)

// Specify Ajax request optionsvar options = {    url: "http://StephenWalther.com"};// Make the Ajax requestvar request = WinJS.xhr(options).then(    function (xmlHttpRequest) {        console.log("success");    },    function ...

Get Windows® 8.1 Apps with HTML5 and JavaScript Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.