Chromium Code Reviews| Index: corelib/src/future.dart |
| diff --git a/corelib/src/future.dart b/corelib/src/future.dart |
| index 9842b8ef65c8fad727c1bed21fa266a25dff63a6..a044700b3f24359530492b7ab258c10defae1b1b 100644 |
| --- a/corelib/src/future.dart |
| +++ b/corelib/src/future.dart |
| @@ -111,3 +111,38 @@ interface Completer<T> factory CompleterImpl<T> { |
| */ |
| void completeException(Object exception); |
| } |
| + |
| + |
| +/** |
| + * This class is for utility functions that operate on Futures (for |
| + * example, waiting for a collectin of Futures to complete). |
|
Siggi Cherem (dart-lang)
2011/11/04 17:35:28
collectin -> collection
Futures -> futures
Ben Laurie (Google)
2011/11/04 17:54:46
or [Future]s
|
| + */ |
| +class Futures { |
| + |
| + /** |
| + * Returns a future which will complete once all the futures in a |
|
Siggi Cherem (dart-lang)
2011/11/04 17:35:28
a future -> a [Future]
|
| + * list are complete. (The value of the returned future will |
| + * be a list of all the values that were produced.) |
| + */ |
| + static Future<List> wait(List<Future> futures) { |
| + Completer completer = new Completer<List>(); |
| + int remaining = futures.length; |
| + List<Object> values = new List(futures.length); |
| + |
| + // As each future completes, put its value into the corresponding |
| + // position in the list of values. |
| + for (int i = 0; i < futures.length; i++) { |
| + // TODO(mattsh) - remove this after bug |
| + // http://code.google.com/p/dart/issues/detail?id=333 is fixed. |
| + int pos = i; |
| + futures[pos].then((Object value) { |
| + values[pos] = value; |
| + if (--remaining == 0) { |
| + completer.complete(values); |
| + } |
| + }); |
| + } |
| + return completer.future; |
| + } |
| +} |
| + |