WinJS Promises

I’m working on an app for Windows 8, and I decided that I would try doing it in HTML5 and Javascript (WinJS), thinking that I’d have an easier time with arranging HTML than with arranging XAML.

I’m pretty sure that isn’t going to be the case as it seems like it’s hard to make my app not look like a formatted webpage (no duh) instead of a native app.  But anyway, the one thing that is cool to see is that MS is making sure that lots of stuff in WinJS uses promises.

A promise is a way to deal with callbacks, but usually in a nicer and more fluent manner.  Rather than messing around with timeouts and callbacks, you can write code like something.execute().then(finish()).done(cleanup());

WinJS has it’s own Promise object.  There is a very nice write up here:

http://stephenwalther.com/archive/2012/02/22/metro-promises.aspx

The best example is this one showing the full life cycle of a WinJS promise:

image

When working in regular javascript (instead of WinJS), you can make use of a number of different promise libraries:

http://api.jquery.com/category/deferred-object/

http://www.commonjs.org/

http://blogs.msdn.com/b/rbuckton/archive/2011/08/15/promise-js-2-0-promise-framework-for-javascript.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s