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 /** | 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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 * will receive it. | 116 * will receive it. |
117 * | 117 * |
118 * If an exception occurs (received by this future, thrown by | 118 * If an exception occurs (received by this future, thrown by |
119 * [transformation], or received by the future returned by [transformation]) | 119 * [transformation], or received by the future returned by [transformation]) |
120 * then the returned future will receive the exception. | 120 * then the returned future will receive the exception. |
121 * | 121 * |
122 * 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 |
123 * chain, and any you add afterwards will not be invoked. | 123 * chain, and any you add afterwards will not be invoked. |
124 */ | 124 */ |
125 Future chain(Future transformation(T value)); | 125 Future chain(Future transformation(T value)); |
| 126 |
| 127 /** |
| 128 * A future representing [transformation] applied to this future's exception. |
| 129 * This can be used to "catch" an exception coming from `this` and translate |
| 130 * it to a more appropriate result. |
| 131 * |
| 132 * If this future gets a value, it simply completes to that same value. If an |
| 133 * exception occurs, then [transformation] will be called with the exception |
| 134 * value. If [transformation] itself throws an exception, then the returned |
| 135 * future completes with that exception. Otherwise, the future will complete |
| 136 * with the value returned by [transformation]. |
| 137 */ |
| 138 Future transformException(transformation(Object exception)); |
126 } | 139 } |
127 | 140 |
128 | |
129 /** | 141 /** |
130 * A [Completer] is used to produce [Future]s and supply their value when it | 142 * A [Completer] is used to produce [Future]s and supply their value when it |
131 * becomes available. | 143 * becomes available. |
132 * | 144 * |
133 * A service that provides values to callers, and wants to return [Future]s can | 145 * A service that provides values to callers, and wants to return [Future]s can |
134 * use a [Completer] as follows: | 146 * use a [Completer] as follows: |
135 * | 147 * |
136 * Completer completer = new Completer(); | 148 * Completer completer = new Completer(); |
137 * // send future object back to client... | 149 * // send future object back to client... |
138 * return completer.future; | 150 * return completer.future; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 future.handleException((exception) { | 231 future.handleException((exception) { |
220 if (!result.isComplete) { | 232 if (!result.isComplete) { |
221 completer.completeException(exception, future.stackTrace); | 233 completer.completeException(exception, future.stackTrace); |
222 } | 234 } |
223 return true; | 235 return true; |
224 }); | 236 }); |
225 } | 237 } |
226 return result; | 238 return result; |
227 } | 239 } |
228 } | 240 } |
OLD | NEW |