| 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 part of dart.async; | 5 part of dart.async; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * An object representing a delayed computation. | 8 * An object representing a delayed computation. |
| 9 * | 9 * |
| 10 * A [Future] is used to obtain a not yet | 10 * A [Future] is used to obtain a not yet |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 * Returns a future which will complete once all the futures in a list are | 187 * Returns a future which will complete once all the futures in a list are |
| 188 * complete. If any of the futures in the list completes with an error, | 188 * complete. If any of the futures in the list completes with an error, |
| 189 * the resulting future also completes with an error. Otherwise the value | 189 * the resulting future also completes with an error. Otherwise the value |
| 190 * of the returned future will be a list of all the values that were produced. | 190 * of the returned future will be a list of all the values that were produced. |
| 191 */ | 191 */ |
| 192 static Future<List> wait(Iterable<Future> futures) { | 192 static Future<List> wait(Iterable<Future> futures) { |
| 193 Completer completer; | 193 Completer completer; |
| 194 // List collecting values from the futures. | 194 // List collecting values from the futures. |
| 195 // Set to null if an error occurs. | 195 // Set to null if an error occurs. |
| 196 List values; | 196 List values; |
| 197 void handleError(error) { | 197 |
| 198 handleError(error) { |
| 198 if (values != null) { | 199 if (values != null) { |
| 199 values = null; | 200 values = null; |
| 200 completer.completeError(error); | 201 completer.completeError(error); |
| 201 } | 202 } |
| 203 return null; |
| 202 } | 204 } |
| 205 |
| 203 // As each future completes, put its value into the corresponding | 206 // As each future completes, put its value into the corresponding |
| 204 // position in the list of values. | 207 // position in the list of values. |
| 205 int remaining = 0; | 208 int remaining = 0; |
| 206 for (Future future in futures) { | 209 for (Future future in futures) { |
| 207 int pos = remaining++; | 210 int pos = remaining++; |
| 208 future.catchError(handleError).then((Object value) { | 211 future.catchError(handleError).then((Object value) { |
| 209 if (values == null) return null; | 212 if (values == null) return null; |
| 210 values[pos] = value; | 213 values[pos] = value; |
| 211 remaining--; | 214 remaining--; |
| 212 if (remaining == 0) { | 215 if (remaining == 0) { |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 */ | 361 */ |
| 359 Stream<T> asStream(); | 362 Stream<T> asStream(); |
| 360 } | 363 } |
| 361 | 364 |
| 362 /** | 365 /** |
| 363 * A way to produce Future objects and to complete them later | 366 * A way to produce Future objects and to complete them later |
| 364 * with a value or error. | 367 * with a value or error. |
| 365 * | 368 * |
| 366 * If you already have a Future, you probably don't need a Completer. | 369 * If you already have a Future, you probably don't need a Completer. |
| 367 * Instead, you can usually use [Future.then], which returns a Future: | 370 * Instead, you can usually use [Future.then], which returns a Future: |
| 368 * | 371 * |
| 369 * Future doStuff(){ | 372 * Future doStuff(){ |
| 370 * return someAsyncOperation().then((result) { | 373 * return someAsyncOperation().then((result) { |
| 371 * // Do something. | 374 * // Do something. |
| 372 * }); | 375 * }); |
| 373 * } | 376 * } |
| 374 * | 377 * |
| 375 * If you do need to create a Future from scratch—for example, | 378 * If you do need to create a Future from scratch—for example, |
| 376 * when you're converting a callback-based API into a Future-based | 379 * when you're converting a callback-based API into a Future-based |
| 377 * one—you can use a Completer as follows: | 380 * one—you can use a Completer as follows: |
| 378 * | 381 * |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 * | 468 * |
| 466 * The argument [exception] must not be `null`. | 469 * The argument [exception] must not be `null`. |
| 467 */ | 470 */ |
| 468 void completeError(Object exception, [Object stackTrace]); | 471 void completeError(Object exception, [Object stackTrace]); |
| 469 | 472 |
| 470 /** | 473 /** |
| 471 * Whether the future has been completed. | 474 * Whether the future has been completed. |
| 472 */ | 475 */ |
| 473 bool get isCompleted; | 476 bool get isCompleted; |
| 474 } | 477 } |
| OLD | NEW |