Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Unified Diff: sdk/lib/async/future.dart

Issue 12091105: Add Future.of that calls a function and captures the result (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Better comment. Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sdk/lib/async/future_impl.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/async/future.dart
diff --git a/sdk/lib/async/future.dart b/sdk/lib/async/future.dart
index f2d926440737fa0f0e519f45eace8a395d21bd4c..0d32aaee8a3daa977ccb35e846ff8a80f36f0af3 100644
--- a/sdk/lib/async/future.dart
+++ b/sdk/lib/async/future.dart
@@ -85,16 +85,50 @@ part of dart.async;
// TODO(floitsch): document chaining.
abstract class Future<T> {
/**
+ * Creates a future containing the result of calling [function].
+ *
+ * The result of computing [:function():] is either a returned value or
+ * a throw.
+ *
+ * If a value is returned, it becomes the result of the created future.
+ *
+ * If calling [function] throws, the created [Future] will be completed
+ * with an async error containing the thrown value and a captured
+ * stacktrace.
+ *
+ * However, if the result of calling [function] is already an asynchronous
+ * result, we treat it specially.
+ *
+ * If the returned value is itself a [Future], completion of
+ * the created future will wait until the returned future completes,
+ * and will then complete with the same result.
+ *
+ * If a thrown value is an [AsyncError], it is used directly as the result
+ * of the created future.
+ */
+ factory Future.of(function()) {
+ try {
+ var result = function();
+ return new _FutureImpl<T>().._setOrChainValue(result);
+ } catch (error, stackTrace) {
+ return new _FutureImpl<T>.immediateError(error, stackTrace);
+ }
+ }
+
+ /**
* A future whose value is available in the next event-loop iteration.
*
- * See [Completer]s, for futures with values that are computed asynchronously.
+ * If [value] is not a [Future], using this constructor is equivalent
+ * to [:new Future.of(() => value):].
+ *
+ * See [Completer] to create a Future and complete it later.
*/
factory Future.immediate(T value) => new _FutureImpl<T>.immediate(value);
/**
* A future that completes with an error in the next event-loop iteration.
*
- * See [Completer]s, for futures with values that are computed asynchronously.
+ * See [Completer] to create a Future and complete it later.
*/
factory Future.immediateError(var error, [Object stackTrace]) {
return new _FutureImpl<T>.immediateError(error, stackTrace);
@@ -113,7 +147,8 @@ abstract class Future<T> {
* See [Completer]s, for futures with values that are computed asynchronously.
*/
factory Future.delayed(int milliseconds, T value()) {
- _ThenFuture<dynamic, T> future = new _ThenFuture<dynamic, T>((_) => value());
+ _ThenFuture<dynamic, T> future =
+ new _ThenFuture<dynamic, T>((_) => value());
new Timer(milliseconds, (_) => future._sendValue(null));
return future;
}
« no previous file with comments | « no previous file | sdk/lib/async/future_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698