OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Dart core library. | 5 // Dart core library. |
6 | 6 |
7 | 7 |
8 /** | 8 /** |
9 * A Future is used to obtain a value sometime in the | 9 * A Future is used to obtain a value sometime in the |
10 * future. | 10 * future. |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 /** | 104 /** |
105 * Called when value is available. | 105 * Called when value is available. |
106 */ | 106 */ |
107 void complete(T value); | 107 void complete(T value); |
108 | 108 |
109 /** | 109 /** |
110 * Called if an exception occured while trying to produce value. | 110 * Called if an exception occured while trying to produce value. |
111 */ | 111 */ |
112 void completeException(Object exception); | 112 void completeException(Object exception); |
113 } | 113 } |
| 114 |
| 115 |
| 116 /** |
| 117 * This class is for utility functions that operate on Futures (for |
| 118 * example, waiting for a collectin of Futures to complete). |
| 119 */ |
| 120 class Futures { |
| 121 |
| 122 /** |
| 123 * Returns a future which will complete once all the futures in a |
| 124 * list are complete. (The value of the returned future will |
| 125 * be a list of all the values that were produced.) |
| 126 */ |
| 127 static Future<List> wait(List<Future> futures) { |
| 128 Completer completer = new Completer<List>(); |
| 129 int remaining = futures.length; |
| 130 List<Object> values = new List(futures.length); |
| 131 |
| 132 // As each future completes, put its value into the corresponding |
| 133 // position in the list of values. |
| 134 for (int i = 0; i < futures.length; i++) { |
| 135 // TODO(mattsh) - remove this after bug |
| 136 // http://code.google.com/p/dart/issues/detail?id=333 is fixed. |
| 137 int pos = i; |
| 138 futures[pos].then((Object value) { |
| 139 values[pos] = value; |
| 140 if (--remaining == 0) { |
| 141 completer.complete(values); |
| 142 } |
| 143 }); |
| 144 } |
| 145 return completer.future; |
| 146 } |
| 147 } |
| 148 |
OLD | NEW |