Chromium Code Reviews| 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. | |
| 6 | |
| 7 | |
| 8 /** | 5 /** |
| 9 * 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 |
| 10 * [Future] can obtain the value by passing a callback to [then]. | 7 * [Future] can obtain the value by passing a callback to [then]. For example: |
| 11 * For example: | |
| 12 * | 8 * |
| 13 * Future<int> future = getFutureFromSomewhere(); | 9 * Future<int> future = getFutureFromSomewhere(); |
| 14 * future.then((value) { | 10 * future.then((value) { |
| 15 * print("I received the number $value"); | 11 * print("I received the number $value"); |
| 16 * }); | 12 * }); |
| 17 * | 13 * |
| 18 * A future may complete by *succeeding* (producing a value) or *failing* | 14 * A future may complete by *succeeding* (producing a value) or *failing* |
| 19 * (producing an exception, which may be handled with [handleException]). | 15 * (producing an exception, which may be handled with [handleException]). |
| 20 * Callbacks passed to [onComplete] will be invoked in either case. | 16 * Callbacks passed to [onComplete] will be invoked in either case. |
| 21 * | 17 * |
| 22 * When a future completes, the following actions happen in order: | 18 * When a future completes, the following actions happen in order: |
| 23 * | 19 * |
| 24 * 1. if the future suceeded, handlers registered with [then] are called. | 20 * 1. if the future suceeded, handlers registered with [then] are called. |
| 25 * 2. if the future failed, handlers registered with [handleException] are | 21 * 2. if the future failed, handlers registered with [handleException] are |
| 26 * called in sequence, until one returns true. | 22 * called in sequence, until one returns true. |
| 27 * 3. handlers registered with [onComplete] are called | 23 * 3. handlers registered with [onComplete] are called |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 40 T get value(); | 36 T get value(); |
| 41 | 37 |
| 42 /** | 38 /** |
| 43 * Exception that occurred ([:null:] if no exception occured). This property | 39 * Exception that occurred ([:null:] if no exception occured). This property |
| 44 * throws a [FutureNotCompleteException] if it is used before this future is | 40 * throws a [FutureNotCompleteException] if it is used before this future is |
| 45 * completes. | 41 * completes. |
| 46 */ | 42 */ |
| 47 Object get exception(); | 43 Object get exception(); |
| 48 | 44 |
| 49 /** | 45 /** |
| 46 * The call stack object associated with the exception that occurred. This | |
| 47 * throws a [FutureNotCompleteException] if it is used before the future | |
| 48 * completes. Returns [:null:] if the future completed successfully or a | |
| 49 * call stack wasn't provided with the exception when it occurred. | |
| 50 */ | |
| 51 Object get callStack(); | |
|
Siggi Cherem (dart-lang)
2012/06/12 18:38:25
callStack => stackTrace?
| |
| 52 | |
| 53 /** | |
| 50 * Whether the future is complete (either the value is available or there was | 54 * Whether the future is complete (either the value is available or there was |
| 51 * an exception). | 55 * an exception). |
| 52 */ | 56 */ |
| 53 bool get isComplete(); | 57 bool get isComplete(); |
| 54 | 58 |
| 55 /** | 59 /** |
| 56 * Whether the value is available (meaning [isComplete] is true, and there was | 60 * Whether the value is available (meaning [isComplete] is true, and there was |
| 57 * no exception). | 61 * no exception). |
| 58 */ | 62 */ |
| 59 bool get hasValue(); | 63 bool get hasValue(); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 90 void handleException(bool onException(Object exception)); | 94 void handleException(bool onException(Object exception)); |
| 91 | 95 |
| 92 /** | 96 /** |
| 93 * A future representing [transformation] applied to this future's value. | 97 * A future representing [transformation] applied to this future's value. |
| 94 * | 98 * |
| 95 * When this future gets a value, [transformation] will be called on the | 99 * When this future gets a value, [transformation] will be called on the |
| 96 * value, and the returned future will receive the result. | 100 * value, and the returned future will receive the result. |
| 97 * | 101 * |
| 98 * If an exception occurs (received by this future, or thrown by | 102 * If an exception occurs (received by this future, or thrown by |
| 99 * [transformation]) then the returned future will receive the exception. | 103 * [transformation]) then the returned future will receive the exception. |
| 100 * | 104 * |
| 101 * You must not add exception handlers to [this] future prior to calling | 105 * You must not add exception handlers to [this] future prior to calling |
| 102 * transform, and any you add afterwards will not be invoked. | 106 * transform, and any you add afterwards will not be invoked. |
| 103 */ | 107 */ |
| 104 Future transform(transformation(T value)); | 108 Future transform(transformation(T value)); |
| 105 | 109 |
| 106 /** | 110 /** |
| 107 * A future representing an asynchronous transformation applied to this | 111 * A future representing an asynchronous transformation applied to this |
| 108 * future's value. [transformation] must return a Future. | 112 * future's value. [transformation] must return a Future. |
| 109 * | 113 * |
| 110 * When this future gets a value, [transformation] will be called on the | 114 * When this future gets a value, [transformation] will be called on the |
| 111 * value. When the resulting future gets a value, the returned future | 115 * value. When the resulting future gets a value, the returned future |
| 112 * will receive it. | 116 * will receive it. |
| 113 * | 117 * |
| 114 * If an exception occurs (received by this future, thrown by | 118 * If an exception occurs (received by this future, thrown by |
| 115 * [transformation], or received by the future returned by [transformation]) | 119 * [transformation], or received by the future returned by [transformation]) |
| 116 * then the returned future will receive the exception. | 120 * then the returned future will receive the exception. |
| 117 * | 121 * |
| 118 * You must not add exception handlers to [this] future prior to calling | 122 * You must not add exception handlers to [this] future prior to calling |
| 119 * chain, and any you add afterwards will not be invoked. | 123 * chain, and any you add afterwards will not be invoked. |
| 120 */ | 124 */ |
| 121 Future chain(Future transformation(T value)); | 125 Future chain(Future transformation(T value)); |
| 122 } | 126 } |
| 123 | 127 |
| 124 | 128 |
| 125 /** | 129 /** |
| 126 * A [Completer] is used to produce [Future]s and supply their value when it | 130 * A [Completer] is used to produce [Future]s and supply their value when it |
| 127 * becomes available. | 131 * becomes available. |
| 128 * | 132 * |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 147 Completer(); | 151 Completer(); |
| 148 | 152 |
| 149 /** The future that will contain the value produced by this completer. */ | 153 /** The future that will contain the value produced by this completer. */ |
| 150 Future get future(); | 154 Future get future(); |
| 151 | 155 |
| 152 /** Supply a value for [future]. */ | 156 /** Supply a value for [future]. */ |
| 153 void complete(T value); | 157 void complete(T value); |
| 154 | 158 |
| 155 /** | 159 /** |
| 156 * Indicate in [future] that an exception occured while trying to produce its | 160 * Indicate in [future] that an exception occured while trying to produce its |
| 157 * value. The argument [exception] should not be [:null:]. | 161 * value. The argument [exception] should not be [:null:]. A [callStack] |
| 162 * object can be provided as well to give the user information about where | |
| 163 * the error occurred. If omitted, it will be [:null:]. | |
| 158 */ | 164 */ |
| 159 void completeException(Object exception); | 165 void completeException(Object exception, [Object callStack]); |
| 160 } | 166 } |
| 161 | 167 |
| 162 /** Thrown when reading a future's properties before it is complete. */ | 168 /** Thrown when reading a future's properties before it is complete. */ |
| 163 class FutureNotCompleteException implements Exception { | 169 class FutureNotCompleteException implements Exception { |
| 164 FutureNotCompleteException() {} | 170 FutureNotCompleteException() {} |
| 165 String toString() => "Exception: future has not been completed"; | 171 String toString() => "Exception: future has not been completed"; |
| 166 } | 172 } |
| 167 | 173 |
| 168 /** | 174 /** |
| 169 * Thrown if a completer tries to set the value on a future that is already | 175 * Thrown if a completer tries to set the value on a future that is already |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 } | 217 } |
| 212 }); | 218 }); |
| 213 future.handleException((exception) { | 219 future.handleException((exception) { |
| 214 if (!result.isComplete) completer.completeException(exception); | 220 if (!result.isComplete) completer.completeException(exception); |
| 215 return true; | 221 return true; |
| 216 }); | 222 }); |
| 217 } | 223 } |
| 218 return result; | 224 return result; |
| 219 } | 225 } |
| 220 } | 226 } |
| OLD | NEW |