| 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 * A [Future] is used to obtain a value sometime in the future. Receivers of a | 8 * A [Future] is used to obtain a value sometime in the future. Receivers of a |
| 9 * [Future] can obtain the value by passing a callback to [then]. For example: | 9 * [Future] can obtain the value by passing a callback to [then]. For example: |
| 10 * | 10 * |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 * | 134 * |
| 135 * // alternatively, if the service cannot produce the value, it | 135 * // alternatively, if the service cannot produce the value, it |
| 136 * // can provide an exception: | 136 * // can provide an exception: |
| 137 * completer.completeException(exception); | 137 * completer.completeException(exception); |
| 138 * | 138 * |
| 139 */ | 139 */ |
| 140 abstract class Completer<T> { | 140 abstract class Completer<T> { |
| 141 | 141 |
| 142 factory Completer() => new _CompleterImpl<T>(); | 142 factory Completer() => new _CompleterImpl<T>(); |
| 143 | 143 |
| 144 /** The future that will contain the value produced by this completer. */ | 144 /** The future that will contain the result provided to this completer. */ |
| 145 Future get future; | 145 Future get future; |
| 146 | 146 |
| 147 /** Supply a value for [future]. */ | 147 /** |
| 148 void complete(T value); | 148 * Completes [future] with the supplied values. |
| 149 * |
| 150 * All listeners on the future will be immediately informed about the value. |
| 151 */ |
| 152 void complete([T value]); |
| 149 | 153 |
| 150 /** | 154 /** |
| 151 * Indicate in [future] that an exception occured while trying to produce its | 155 * Complete [future] with an error. |
| 152 * value. The argument [exception] should not be [:null:]. A [stackTrace] | 156 * |
| 153 * object can be provided as well to give the user information about where | 157 * Completing a future with an error indicates that an exception was thrown |
| 158 * while trying to produce a value. |
| 159 * |
| 160 * The argument [exception] should not be [:null:]. A [stackTrace] |
| 161 * object can be provided as well, to give the user information about where |
| 154 * the error occurred. If omitted, it will be [:null:]. | 162 * the error occurred. If omitted, it will be [:null:]. |
| 155 */ | 163 */ |
| 156 void completeError(Object exception, [Object stackTrace]); | 164 void completeError(Object exception, [Object stackTrace]); |
| 157 } | 165 } |
| 158 | 166 |
| 159 class Futures { | 167 class Futures { |
| 160 /** | 168 /** |
| 161 * Returns a future which will complete once all the futures in a list are | 169 * Returns a future which will complete once all the futures in a list are |
| 162 * complete. If any of the futures in the list completes with an exception, | 170 * complete. If any of the futures in the list completes with an exception, |
| 163 * the resulting future also completes with an exception. (The value of the | 171 * the resulting future also completes with an exception. (The value of the |
| (...skipping 13 matching lines...) Expand all Loading... |
| 177 */ | 185 */ |
| 178 static Future forEach(Iterable input, Future f(element)) { | 186 static Future forEach(Iterable input, Future f(element)) { |
| 179 var iterator = input.iterator; | 187 var iterator = input.iterator; |
| 180 Future nextElement(_) { | 188 Future nextElement(_) { |
| 181 if (!iterator.moveNext()) return new Future.immediate(null); | 189 if (!iterator.moveNext()) return new Future.immediate(null); |
| 182 return f(iterator.current).then(nextElement); | 190 return f(iterator.current).then(nextElement); |
| 183 } | 191 } |
| 184 return nextElement(null); | 192 return nextElement(null); |
| 185 } | 193 } |
| 186 } | 194 } |
| OLD | NEW |