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.