BT

Dart Adds Support for Asynchronous Programming

| by James Chesters Follow 1 Followers on Mar 30, 2015. Estimated reading time: 2 minutes |

Google has released Dart 1.9, bringing fresh support for asynchronous programming.

Kevin Moore, product manager for Google, said the release of version 1.9 introduces async methods and await expressions built on top of its existing Future API.

The open source web programming language has so far used the Future API to help compose asynchronous operations, but Moore says handling conditional cases and errors has still been difficult.

In the post Dart 1.9: The release you’ve been await-ing for Moore elaborates on the the improvements, saying developers "can now use familiar control flow features – for/while loops, if blocks, and try/catch – to manage complex asynchronous interactions."

In the article Dart Language Asynchrony Support: Phase 1 Google software engineer Gilad Bracha demonstrates the advantages of using async and await.

The first example is without using async and await:

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 it isn't too complicated, it also isn't "totally simple," compared to what can be done with the new language features:

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);
  }
}

Further to the async methods and await expressions, Moore says that the team is also introducing generator methods – sync* and async* – "that make it easy to lazily generate sequences" that he says aims to "eliminate almost all cases where developers need to create custom iterators or manually manage stream creation."

Dart 1.9 brings several other important updates -- including full support for enum, and some core library changes. Moore lists as highlights "New model for shared server sockets – no need for Socket reference, implemented on all platforms" as well as an improved regexp engine.

For the dart:io API, the changes include:

  • ServerSocket.bind, RawServerSocket.bind, SecureServerSocket.bind and RawSecureServerSocket.bind methods added new argument shared.
  • SocketReference methods and classes have been marked as deprecated.
  • Socket and RawSocket static method connect added sourceAddress argument.

The news of the release was largely greeted warmly by the developer community. In the related discussion on Hacker News, Thomas Schranz, co-founder of blossom.io, commented: "This is, IMHO, the most exciting release since 1.0."

Schranz's sentiment was echoed by user ahoge, who said "This is the biggest release since 1.0. Async/await and generators are a welcome addition. Shareable server sockets and better isolates are also nice to have."

Google's 1.9 release for Dart comes just a day after the announcement that it would be focusing web efforts on compiling Dart to JavaScript, and that the decision had been made not to integrate the Dart VM into Chrome.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT