| 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 future. Receivers of a | 9 * A [Future] is used to obtain a value sometime in the future. Receivers of a |
| 10 * [Future] can obtain the value by passing a callback to [then]. | 10 * [Future] can obtain the value by passing a callback to [then]. |
| 11 * For example: | 11 * For example: |
| 12 * | 12 * |
| 13 * Future<int> future = getFutureFromSomewhere(); | 13 * Future<int> future = getFutureFromSomewhere(); |
| 14 * future.then((value) { | 14 * future.then((value) { |
| 15 * print("I received the number $value"); | 15 * print("I received the number $value"); |
| 16 * }); | 16 * }); |
| 17 * | 17 * |
| 18 * A future may complete by *succeeding* (producing a value) or *failing* | 18 * A future may complete by *succeeding* (producing a value) or *failing* |
| 19 * (producing an exception, which may be handled with [handleException]). | 19 * (producing an exception, which may be handled with [handleException]). |
| 20 * Callbacks passed to [onComplete] will be invoked in either case. | 20 * Callbacks passed to [onComplete] will be invoked in either case. |
| 21 * | 21 * |
| 22 * When a future completes: | 22 * When a future completes, the following actions happen in order: |
| 23 * | 23 * |
| 24 * 1. if the future suceeded, handlers registered with [then] are called. | 24 * 1. if the future suceeded, handlers registered with [then] are called. |
| 25 * 2. if the future failed, handlers registered with [handleException] are | 25 * 2. if the future failed, handlers registered with [handleException] are |
| 26 * called in sequence, until one returns true. | 26 * called in sequence, until one returns true. |
| 27 * 3. handlers registered with [onComplete] are called | 27 * 3. handlers registered with [onComplete] are called |
| 28 * 4. if the future failed, and at least one handler was registered with | 28 * 4. if the future failed, and at least one handler was registered with |
| 29 * [then], and no handler registered with [handleException] returned | 29 * [then], and no handler registered with [handleException] returned |
| 30 * [:true:], then the exception is thrown. | 30 * [:true:], then the exception is thrown. |
| 31 * | 31 * |
| 32 * Use a [Completer] to create and change the state of a [Future]. | 32 * Use a [Completer] to create and change the state of a [Future]. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 62 * When this future is complete (either with a value or with an exception), | 62 * When this future is complete (either with a value or with an exception), |
| 63 * then [complete] is called with the future. | 63 * then [complete] is called with the future. |
| 64 * If [complete] throws an exception, it is ignored. | 64 * If [complete] throws an exception, it is ignored. |
| 65 */ | 65 */ |
| 66 void onComplete(void complete(Future<T> future)); | 66 void onComplete(void complete(Future<T> future)); |
| 67 | 67 |
| 68 /** | 68 /** |
| 69 * If this future is complete and has a value, then [onValue] is called | 69 * If this future is complete and has a value, then [onValue] is called |
| 70 * with the value. | 70 * with the value. |
| 71 */ | 71 */ |
| 72 void then(void onValue(T value)); | 72 void then(void onSuccess(T value)); |
| 73 | 73 |
| 74 /** | 74 /** |
| 75 * If this future is complete and has an exception, then call [onException]. | 75 * If this future is complete and has an exception, then call [onException]. |
| 76 * | 76 * |
| 77 * If [onException] returns true, then the exception is considered handled. | 77 * If [onException] returns true, then the exception is considered handled. |
| 78 * | 78 * |
| 79 * If [onException] does not return true (or [handleException] was never | 79 * If [onException] does not return true (or [handleException] was never |
| 80 * called), then the exception is not considered handled. In that case, if | 80 * called), then the exception is not considered handled. In that case, if |
| 81 * there were any calls to [then], then the exception will be thrown when the | 81 * there were any calls to [then], then the exception will be thrown when the |
| 82 * value is set. | 82 * value is set. |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 } | 205 } |
| 206 }); | 206 }); |
| 207 future.handleException((exception) { | 207 future.handleException((exception) { |
| 208 if (!result.isComplete) completer.completeException(exception); | 208 if (!result.isComplete) completer.completeException(exception); |
| 209 return true; | 209 return true; |
| 210 }); | 210 }); |
| 211 } | 211 } |
| 212 return result; | 212 return result; |
| 213 } | 213 } |
| 214 } | 214 } |
| OLD | NEW |