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

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: WIth tests and bugfixes 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 | tests/lib/async/future_test.dart » ('j') | tests/lib/async/future_test.dart » ('J')
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..14285b516baadffb97acab02942c131cc293aaa9 100644
--- a/sdk/lib/async/future.dart
+++ b/sdk/lib/async/future.dart
@@ -85,6 +85,24 @@ 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 used to return
+ * a completed future with that result.
+ * If [function] returns a [Future], this will create a future that
+ * will eventually complete with the same result.
floitsch 2013/02/01 12:44:11 Maybe explain it in contrast to "immediate". This
Lasse Reichstein Nielsen 2013/02/01 13:16:50 I think I'll do it the opposite way: new Future.i
+ */
+ factory Future.of(function()) {
+ try {
+ var result = function();
+ if (result is Future) return new _FutureWrapper<T>(result);
floitsch 2013/02/01 12:44:11 should we wrap?
Lasse Reichstein Nielsen 2013/02/01 13:16:50 That was my question too. I decided to do it just
+ return new _FutureImpl<T>.immediate(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.
« no previous file with comments | « no previous file | tests/lib/async/future_test.dart » ('j') | tests/lib/async/future_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698