Creating Promises

If your app has a function that takes more then a few milliseconds to complete then you should return a promise so that your app can continue to work while the function is completing. You can create your own promises by creating a new instance of the Promise class. The constructor for the Promise class requires a function that accepts three parameters: a complete, error, and progress function parameter.

For example, the code in Listing 2.10 illustrates how you can create a method named wait10Seconds() which returns a promise. The progress function is called every second and the complete function is not called until 10 seconds have passed.

LISTING 2.10 Creating a Promise (promiseCreate\promiseCreate.html)

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.