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 represent a potential value, or error, | 10 * A [Future] is used to represent a potential value, or error, |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 * complete before the returned future is completed (still with the first | 247 * complete before the returned future is completed (still with the first |
248 * error to occur, the remaining errors are silently dropped). | 248 * error to occur, the remaining errors are silently dropped). |
249 * | 249 * |
250 * If [cleanUp] is provided, in the case of an error, any non-null result of | 250 * If [cleanUp] is provided, in the case of an error, any non-null result of |
251 * a successful future is passed to `cleanUp`, which can then release any | 251 * a successful future is passed to `cleanUp`, which can then release any |
252 * resources that the successful operation allocated. | 252 * resources that the successful operation allocated. |
253 * | 253 * |
254 * The call to `cleanUp` should not throw. If it does, the error will be an | 254 * The call to `cleanUp` should not throw. If it does, the error will be an |
255 * uncaught asynchronous error. | 255 * uncaught asynchronous error. |
256 */ | 256 */ |
257 static Future<List> wait(Iterable<Future> futures, | 257 static Future<List/*<T>*/> wait/*<T>*/(Iterable<Future/*<T>*/> futures, |
258 {bool eagerError: false, | 258 {bool eagerError: false, |
259 void cleanUp(successValue)}) { | 259 void cleanUp(/*=T*/ successValue)}) { |
260 final _Future<List> result = new _Future<List>(); | 260 final _Future<List/*<T>*/> result = new _Future<List/*<T>*/>(); |
261 List values; // Collects the values. Set to null on error. | 261 List/*<T>*/ values; // Collects the values. Set to null on error. |
262 int remaining = 0; // How many futures are we waiting for. | 262 int remaining = 0; // How many futures are we waiting for. |
263 var error; // The first error from a future. | 263 var error; // The first error from a future. |
264 StackTrace stackTrace; // The stackTrace that came with the error. | 264 StackTrace stackTrace; // The stackTrace that came with the error. |
265 | 265 |
266 // Handle an error from any of the futures. | 266 // Handle an error from any of the futures. |
267 void handleError(theError, theStackTrace) { | 267 void handleError(theError, theStackTrace) { |
268 remaining--; | 268 remaining--; |
269 if (values != null) { | 269 if (values != null) { |
270 if (cleanUp != null) { | 270 if (cleanUp != null) { |
271 for (var value2 in values) { | 271 for (var value2 in values) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 } | 305 } |
306 if (remaining == 0 && !eagerError) { | 306 if (remaining == 0 && !eagerError) { |
307 result._completeError(error, stackTrace); | 307 result._completeError(error, stackTrace); |
308 } | 308 } |
309 } | 309 } |
310 }, onError: handleError); | 310 }, onError: handleError); |
311 } | 311 } |
312 if (remaining == 0) { | 312 if (remaining == 0) { |
313 return new Future.value(const []); | 313 return new Future.value(const []); |
314 } | 314 } |
315 values = new List(remaining); | 315 values = new List/*<T>*/(remaining); |
316 return result; | 316 return result; |
317 } | 317 } |
318 | 318 |
319 /** | 319 /** |
320 * Perform an async operation for each element of the iterable, in turn. | 320 * Perform an async operation for each element of the iterable, in turn. |
321 * | 321 * |
322 * Runs [f] for each element in [input] in order, moving to the next element | 322 * Runs [f] for each element in [input] in order, moving to the next element |
323 * only when the [Future] returned by [f] completes. Returns a [Future] that | 323 * only when the [Future] returned by [f] completes. Returns a [Future] that |
324 * completes when all elements have been processed. | 324 * completes when all elements have been processed. |
325 * | 325 * |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 * the future returned by `then` will be completed with | 400 * the future returned by `then` will be completed with |
401 * the same result as the future returned by the callback. | 401 * the same result as the future returned by the callback. |
402 * | 402 * |
403 * If [onError] is not given, and this future completes with an error, | 403 * If [onError] is not given, and this future completes with an error, |
404 * the error is forwarded directly to the returned future. | 404 * the error is forwarded directly to the returned future. |
405 * | 405 * |
406 * In most cases, it is more readable to use [catchError] separately, possibly | 406 * In most cases, it is more readable to use [catchError] separately, possibly |
407 * with a `test` parameter, instead of handling both value and error in a | 407 * with a `test` parameter, instead of handling both value and error in a |
408 * single [then] call. | 408 * single [then] call. |
409 */ | 409 */ |
410 Future then(onValue(T value), { Function onError }); | 410 Future/*<S>*/ then/*<S>*/(/*=S*/ onValue(T value), { Function onError }); |
411 | 411 |
412 /** | 412 /** |
413 * Handles errors emitted by this [Future]. | 413 * Handles errors emitted by this [Future]. |
414 * | 414 * |
415 * This is the asynchronous equivalent of a "catch" block. | 415 * This is the asynchronous equivalent of a "catch" block. |
416 * | 416 * |
417 * Returns a new [Future] that will be completed with either the result of | 417 * Returns a new [Future] that will be completed with either the result of |
418 * this future or the result of calling the `onError` callback. | 418 * this future or the result of calling the `onError` callback. |
419 * | 419 * |
420 * If this future completes with a value, | 420 * If this future completes with a value, |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
720 if (replacement != null) { | 720 if (replacement != null) { |
721 error = _nonNullError(replacement.error); | 721 error = _nonNullError(replacement.error); |
722 stackTrace = replacement.stackTrace; | 722 stackTrace = replacement.stackTrace; |
723 } | 723 } |
724 result._completeError(error, stackTrace); | 724 result._completeError(error, stackTrace); |
725 } | 725 } |
726 | 726 |
727 /** Helper function that converts `null` to a [NullThrownError]. */ | 727 /** Helper function that converts `null` to a [NullThrownError]. */ |
728 Object _nonNullError(Object error) => | 728 Object _nonNullError(Object error) => |
729 (error != null) ? error : new NullThrownError(); | 729 (error != null) ? error : new NullThrownError(); |
OLD | NEW |