OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /** |
6 * A [Future] is used to obtain a value sometime in the future. Receivers of a | 6 * A [Future] is used to obtain a value sometime in the future. Receivers of a |
7 * [Future] can obtain the value by passing a callback to [then]. For example: | 7 * [Future] can obtain the value by passing a callback to [then]. For example: |
8 * | 8 * |
9 * Future<int> future = getFutureFromSomewhere(); | 9 * Future<int> future = getFutureFromSomewhere(); |
10 * future.then((value) { | 10 * future.then((value) { |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 * example, waiting for a collection of Futures to complete). | 221 * example, waiting for a collection of Futures to complete). |
222 */ | 222 */ |
223 class Futures { | 223 class Futures { |
224 /** | 224 /** |
225 * Returns a future which will complete once all the futures in a list are | 225 * Returns a future which will complete once all the futures in a list are |
226 * complete. If any of the futures in the list completes with an exception, | 226 * complete. If any of the futures in the list completes with an exception, |
227 * the resulting future also completes with an exception. (The value of the | 227 * the resulting future also completes with an exception. (The value of the |
228 * returned future will be a list of all the values that were produced.) | 228 * returned future will be a list of all the values that were produced.) |
229 */ | 229 */ |
230 static Future<List> wait(List<Future> futures) { | 230 static Future<List> wait(List<Future> futures) { |
231 if (futures.isEmpty()) { | 231 if (futures.isEmpty) { |
232 return new Future<List>.immediate(const []); | 232 return new Future<List>.immediate(const []); |
233 } | 233 } |
234 | 234 |
235 Completer completer = new Completer<List>(); | 235 Completer completer = new Completer<List>(); |
236 Future<List> result = completer.future; | 236 Future<List> result = completer.future; |
237 int remaining = futures.length; | 237 int remaining = futures.length; |
238 List values = new List(futures.length); | 238 List values = new List(futures.length); |
239 | 239 |
240 // As each future completes, put its value into the corresponding | 240 // As each future completes, put its value into the corresponding |
241 // position in the list of values. | 241 // position in the list of values. |
(...skipping 11 matching lines...) Expand all Loading... |
253 future.handleException((exception) { | 253 future.handleException((exception) { |
254 if (!result.isComplete) { | 254 if (!result.isComplete) { |
255 completer.completeException(exception, future.stackTrace); | 255 completer.completeException(exception, future.stackTrace); |
256 } | 256 } |
257 return true; | 257 return true; |
258 }); | 258 }); |
259 } | 259 } |
260 return result; | 260 return result; |
261 } | 261 } |
262 } | 262 } |
OLD | NEW |