For the latest Dart news, visit our new blog at https://medium.com/dartlang .
Posted by Mads Ager
We heard your feedback for an easier-to-use I/O library, so we just landed a change to make dart:io use futures for single-shot async methods.
For example, where you used to write:
var f = new File('myfile');
f.onError = (e) => print(e);
f.exists((b) => print('exists: $b'));
you now write:
var f = new File('myfile');
var existsFuture = f.exists();
existsFuture.then((b) => print('exists: $b'));
existsFuture.handleException(( e) {
print(e);
return true;
});
This change has multiple advantages:
As always, please share your feedback with us in the Dart mailing list. Thanks for trying Dart!
We heard your feedback for an easier-to-use I/O library, so we just landed a change to make dart:io use futures for single-shot async methods.
For example, where you used to write:
var f = new File('myfile');
f.onError = (e) => print(e);
f.exists((b) => print('exists: $b'));
you now write:
var f = new File('myfile');
var existsFuture = f.exists();
existsFuture.then((b) => print('exists: $b'));
existsFuture.handleException((
print(e);
return true;
});
This change has multiple advantages:
- It is more consistent with the rest of the Dart APIs to use Futures.
- File and Directory objects are now immutable.
- Error handling can be done locally per operation instead of having a global error handler and you get the benefit of the error handling mechanisms built into futures.
As always, please share your feedback with us in the Dart mailing list. Thanks for trying Dart!