days
-4
-5
hours
-1
-1
minutes
-2
-6
seconds
-4
-7
search
JAX Developers Puzzle deadline approaching: Play for a chance to win a complimentary JAX London 2018 ticket!
The a-wait is over

Dart 1.9: Asynchronous programming ahoy

Natali Vlatko
Dart board image via Shutterstock

Google have labelled the new Dart 1.9 release as worth the a-wait and a bunch of developers couldn’t be more chuffed. Now with async methods and await expressions, the team is touting the update as bigger than ever.

The latest release of Dart 1.9 has been hailed as the one we’ve all been await-ing for, with the introduction of async methods and await expressions built on top of their existing Future API.

After news broke recently about Dart’s new, focused strategy to compile into JavaScript, on top of the decision to no longer integrate the Dart VM into Chrome, the announcement has been welcomed by a number of developers already using the language.

The old and the new

Dart 1.9 brings with it the ability to use control flow features such as for/while loops, if blocks, and try/catch to manage complex asynchronous interactions. A more thorough look-through is given in the Dart Language Asynchrony Support Phase 1 article, in which Gilad Bracha shows us how Dart looks before and after the use of async functions and await expressions.

The example below shows Bracha running a simple animation which updates the display on every frame and doesn’t contain the new language features:

import "dart:html"

main() {
  var context = querySelector("canvas").context2D;
  var running = true;    // Set false to stop.

  tick(time) {
    context.clearRect(0, 0, 500, 500);
    context.fillRect(time % 450, 20, 50, 50);

    if (running) window.animationFrame.then(tick);
  }

  window.animationFrame.then(tick);
}

Bracha says that while the above code isn’t complicated, the process isn’t exactly simple, either:

We produce a frame; when the frame is done, we expect to invoke a callback function, tick(), that produces the next frame (if the animation hasn’t been stopped) and passes itself as the callback recursively, perpetuating the process. The function tick() represents the continuation of the computation, and we all know how intuitive and easy continuations are.

The following can be written with Dart’s introduction of async and await:

import "dart:html";

main() async {
  var context = querySelector("canvas").context2D;
  var running = true;    // Set false to stop game.

  while (running) {
    var time = await window.animationFrame;
    context.clearRect(0, 0, 500, 500);
    context.fillRect(time % 450, 20, 50, 50);
  }
}

The above code isn’t preceded by an explanation, with Bracha calling the code “self explanatory”.

Bringing more to the Asynchronous party

Kevin Moore was also excited to introduce the generator methods sync* and async*, with Bracha explaining these advanced asynchrony topics in a follow-up Phase 2 article. Moore states that their addition will make it “easy to lazily generate sequences, eliminating almost all cases where developers need to create custom iterators or manually manage stream creation”.

For interested parties, a complete tour of Dart’s new Asynchrony features is available as a high-level overview.

The release notes are home to a bevy of other enhancements shipping with this release:

  • We now have enum, labelled as a long-requested feature, fully supported
  • An update to the regular expression engine has been made for the Dart VM, now up to 150x faster
  • Core library changes mean Isolate APIs now work across the VM and dart2js

Dart and Google are calling the release the most exciting version since 1.0. Nevertheless, a few developers are still bummed about the new strategy, meaning some interest has waned in Dart’s exploits. What say ye?

Author
Natali Vlatko
An Australian who calls Berlin home, via a two year love affair with Singapore. Natali was an Editorial Assistant for JAXenter.com (S&S Media Group).

Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of