Use yielding (await) in javascript today, with traceur!
Javascript is pretty annoying sometimes, especially when writing a lot of callbacks.
If you’re feeling adventurous, try out the ultra awesome traceur compiler which lets you use ECMAScript Harmony features today.
Boilerplate code (traceur.js and bootstrap.js from traceur)
The “traceur.options.experimental” is to enable the “await” keyword, which is what we’re going to show off.
And now some boilerplate code to wrap the async XMLHttpRequest into a “Deferred” (a Promise).
Now here’s the cool part that we’ve been waiting for. You can now write
code that looks synchronous, but doesn’t block the event loop. This is
a definite readability win, as opposed to minor gains you get with
things like CoffeeScript.
The Grand Finale
If you’re already compiling your application into javascript, you
might as well do it with traceur and get some actual cool features (unlike
CoffeeScript, which only gives you ambiguous looking syntax that omits
parenthesis with function calls, ick)
Have fun, and be safe!
Here is what the compiled code for run() ends up looking like: