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

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

Issue 1563223002: Add Future.any and Stream.fromFutures. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Address comment Created 4 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 | « CHANGELOG.md ('k') | sdk/lib/async/stream.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 bcc20192b1471ae59e7604f04965b760dcdbbea6..48f968fe040af33f87863c3beaf7b7cff1653a53 100644
--- a/sdk/lib/async/future.dart
+++ b/sdk/lib/async/future.dart
@@ -317,6 +317,31 @@ abstract class Future<T> {
}
/**
+ * Returns the result of the first future in [futures] to complete.
+ *
+ * The returned future is completed with the result of the first
+ * future in [futures] to report that it is complete.
+ * The results of all the other futures are discarded.
+ *
+ * If [futures] is empty, or if none of its futures complete,
+ * the returned future never completes.
+ */
+ static Future/*<T>*/ any/*<T>*/(Iterable<Future/*<T>*/> futures) {
+ var completer = new Completer.sync();
+ var onValue = (value) {
+ if (!completer.isCompleted) completer.complete(value);
+ };
+ var onError = (error, stack) {
+ if (!completer.isCompleted) completer.completeError(error, stack);
+ };
+ for (var future in futures) {
+ future.then(onValue, onError: onError);
+ }
+ return completer.future;
+ }
+
+
+ /**
* Perform an async operation for each element of the iterable, in turn.
*
* Runs [f] for each element in [input] in order, moving to the next element
« no previous file with comments | « CHANGELOG.md ('k') | sdk/lib/async/stream.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698