Open Source Resources for Web Application Developers

Shopify - Online Store Builder
Follow Us on Social Sites
Subscribe RSS Subscribe Newsletter Like us on Facebook Follow us on Twitter Follow us on Google+
  • Home
  • News
  • Plugins
  • Design
  • Others
  • Books
  • About
04 Jan

JavaScript Asynchronous for Windows Store Apps

  • Information
  • Leave a Comment

JavaScript is a single-threaded language. This means that invoking a long-running process blocks all execution until that process completes. UI elements are unresponsive, animations pause, and no other code in the app can run. The solution to this problem is to avoid synchronous execution as much as possible.

apps-for-windows-8

One way to do this is to have a function execute at a later time, as with event handlers, which are invoked after another call has raised an event. Callback functions are another kind of asynchronous processing, because they call back into the code that initiated the process.

Problems when using asynchronous programming

Asynchronous programming can quickly become complicated. Many of the standard JavaScript APIs rely heavily on callbacks, which are often nested, making them difficult to debug. In addition, the use of anonymous inline functions can make reading the call stack problematic. Exceptions that are thrown from within a heavily nested set of callbacks might not be propagated up to a function that initiated the chain. This makes it difficult to determine exactly where a bug is hidden.

Asynchronous programming in Windows Store apps that use JavaScript

Windows Runtime and Windows Library for JavaScript provide a way to overcome these problems by implementing the Common JS Promises/A proposal. A promise object returns a value at some time in the future. You can use these objects to construct chains of asynchronous code that are easier to read.

All of the Windows Runtime APIs that are exposed to Windows Store apps are wrapped in promise objects. This allows you to use Windows Runtime APIs in a way that you’re comfortable with. The Promises in Windows Library for JavaScript are important because many interactions with Windows Runtime are asynchronous.

Promises explained

A promise is an object. The most frequently used method on a promise object is then, which takes three parameters: a function to call when the promise completes successfully, a function to call when the promise completes with an error, and a function to provide progress information. In both the Windows Runtime and the Windows Library for JavaScript you can also use the done function, which takes the same parameters. The difference is that in the case of an error in processing, the then function returns a promise in the error state but does not throw an exception, while the done method throws an exception if an error function is not provided.

Let’s look at a basic example of a promise with two (made-up) asynchronous methods, one to call a web service and another to store the results in a database.

myWebService.get("http://www.example.com")

.then(function(result) {

return myDb.add(result);

})

.then(function() {

console.log('data successfully saved');

}, function(error) {

console.log('an error occurred while saving:');

console.dir(error);

});

You append the then function to the myWebService.get method. Since this method returns a promise, you can append a second then function to the myDb.add method, and handle any errors in the error function you pass to the second then function.

You can also group multiple promises. For example, you can use the join function run multiple asynchronous operations that call several web services and then aggregate the results.

function loadData() {

var urls = [

'http://www.example.com/',

'http://www.example.org/',

'http://www.example.net'

];

var promises = urls.map(function (url) {

return myWebService.get(url);

});

WinJS.Promise.join(promises).then(function () {

//do the aggregation here.

});

}

First we create an array of URLs, then we use the Array.map function to convert the array of URLs into an array of promises (because mywebService.get returns a promise). Finally, we use the join function, which accepts an array of promises and returns a new promise after all of the promises have completed. We do the data aggregation inside the complete function of the then method.

About the Author

This tutorial is brought to you by the team at MSDN. To learn more about coding for Windows Store apps, please visit http://dev.windows.com

Share
Tweet
Sponsors
subscribe to our newsletter - weekly free resouces for web developers
follow us on social sites - rss, facebook, google+, Twitter
Subscribe RSS Like us on Facebook Follow us on Twitter Follow us on Google+
Related Resources
Windows Store Apps VS Traditional Web Apps

Windows Store Apps VS Traditional Web Apps

Quickstart: Adding HTML Controls in Windows Store App

Quickstart: Adding HTML Controls in Windows Store App

How to Diagnose JavaScript Errors Faster with Error.stack

How to Diagnose JavaScript Errors Faster with Error.stack

Google Gear Enables Offline Use Of Web Applications

Google Gear Enables Offline Use Of Web Applications

Adding WinJS Controls to a Windows Store App

Adding WinJS Controls to a Windows Store App

How to Create a Windows Store app using HTML and JS

How to Create a Windows Store app using HTML and JS

Comments

Open Source Resources for You

What we need is a list of the top quality resources, so that we can spend more time on our web development. WebAppers only picks the top quality web development resources for you.

© Copyright 2012 WebAppers | About | Archives | Privacy Policy | Advertise | Contact

Sponsors
Advertise Here
Search
By Keywords
30 Days / All Time
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • Professional Business Cards Made By Designers
  • Best Free Chrome Extensions for Web Developers
  • All of the Best Free WordPress Themes You Need
  • How to Create a Web App Admin User Interface
  • Nice List of Open Source Fish Eye Menu
  • Best Web Admin Templates
  • 25 Useful Blogs for Web Design & Development
  • Simple Javascript Progress Bar with CSS
  • 10 Useful & Quality Design Resources
  • Free Web Application Icons
  • Free Responsive HTML5 + CSS3 Site Templates
  • FROONT: Responsive Web Design in the Visual Way
  • Designing Experiences for Responsive Web Sites
  • jQuery Unveil: The Lightweight Version of Lazy Load
  • What are HTML5 Datalists and When to Use Them
  • Easy Responsive Tabs jQuery Plugin for Web & Mobile
  • jQuery.Swatches Turns Div into a Sweet Color Swatch
  • Pure: A Set of Small & Responsive CSS Modules
  • Create Sexy Android-like UI with Fries
  • Creative and Modern Button Styles and Effects
Sponsors
Plugins
  • Advertisement15
  • Calendar48
  • Capture19
  • Charts55
  • Chat22
  • Demo Tour17
  • Gallery121
  • Maps30
  • Menu87
  • Polls9
  • Popup50
  • Tooltips42
  • Upload33
  • Video18
Desgin
  • Brushes11
  • Buttons27
  • Color Schemes25
  • Fonts47
  • Forms115
  • Icons110
  • Patterns24
  • PS Tutorials17
  • Stock Photos21
  • Tables25
Others
  • Announcement104
  • Best Collections6
  • Code57
  • eCommerce25
  • Framework217
  • Hosting13
  • Information246
  • Inspiration32
  • Legal Documents10
  • Reviews8
  • Security13
  • Social28
  • Sound16
  • Stats41
  • Tools302
  • Webmail14
Licesnes
  • BSD License69
  • CC License124
  • GPL License224
  • LGPL License40
  • License Free749
  • MIT License423
Sponsors
Advertise Here
Partners
MaxCDN