default.js 下的 setPromise(WinJS.UI.processAll());

If Rating were a standard HTML control, you could add your event handler after this call to WinJS.UI.processAll. But it's a little more complicated for a Windows Library for JavaScript control like our Rating. Because WinJS.UI.processAllcreates the Rating control for us, we can't add the event handler to Rating until after WinJS.UI.processAll has finished its processing.

If WinJS.UI.processAll were a typical method, we could register the Rating event handler right after we call it. But theWinJS.UI.processAll method is asynchronous, so any code that follows it might run before WinJS.UI.processAllcompletes. So, what do we do? We use a Promise object to receive notification when WinJS.UI.processAll completes.

Like all asynchronous Windows Library for JavaScript methods, WinJS.UI.processAll returns a Promise object. APromise is a "promise" that something will happen in the future; when that thing happens, the Promise is said to have completed.

Promise objects have a then method that takes a "completed" function as a parameter. The Promise calls this function when it completes.

By adding your code to a "completed" function and passing it to the Promise object's then method, you can be sure your code executes after WinJS.UI.processAll is complete.

原文地址:https://www.cnblogs.com/yk00/p/3016839.html