Dart 1.9 Now With Async And Await |
Written by Ian Elliot |
Monday, 30 March 2015 |
The latest release of Dart puts its weight behind the async and await approach to implementing asynchronous code. This really is the only sensible way to do the job.
The big problem with asynchronous code is that it makes a mess of the logical flow of your program. Each time something happens that you have to wait for you have to supply a callback and your little bit of logic comes to an end. Promises and Futures help with the problem, but they too distort the flow of control and you still quickly end up with a deeply nested mess. The best solution as proposed by C# and now Dart is to introduce two new async and await expressions. They have been implemented in Dart using the existing Future API. Using await/async you can now write asynchronous code in a natual manner. For example:
This has the usual look of synchronous flow of control, but the first await suspends the execution of the code and releases the thread to serve events and the rest of the UI. When the asynchronous call is completed the code resumes complete with its state at the next instruction and so on. Notice that you can include asynchronous Once you realize how simple and straightforward asynchronous code becomes with the use of async and await you really can't understand why other languages don't introduce them. The good news is that the next version of JavaScript a.k.a ECMAScript 2016 should include async/await. As well as async and await there are a few other enhancements:
Given the recent announcment that Google won't be adding the Dart VM to Chrome, you need to keep in mind that all of these new features are going to be compiled to JavaScript. Dart 1.9 will run on IE 9,10 and 11, and the latest versions of Firefox, Chrome and Safari. There is of course serverside Dart, where the problems of having to work with JavaScript vanish. More InformationDart 1.9: The release you’ve been await-ing for Related ArticlesDart Changes Course - No Longer To Be Native In Chrome Dart 1.8 Adds Support for App Engine and enums Angular Partners With TypeScript
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
Comments
or email your comment to: comments@i-programmer.info
|
Last Updated ( Monday, 30 March 2015 ) |