Chromium Code Reviews| 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 18 matching lines...) Expand all Loading... | |
| 29 */ | 29 */ |
| 30 abstract class Future<T> { | 30 abstract class Future<T> { |
| 31 /** A future whose value is immediately available. */ | 31 /** A future whose value is immediately available. */ |
| 32 factory Future.immediate(T value) => new _FutureImpl<T>.immediate(value); | 32 factory Future.immediate(T value) => new _FutureImpl<T>.immediate(value); |
| 33 | 33 |
| 34 /** A future that completes with an error. */ | 34 /** A future that completes with an error. */ |
| 35 factory Future.immediateError(var error, [Object stackTrace]) { | 35 factory Future.immediateError(var error, [Object stackTrace]) { |
| 36 return new _FutureImpl<T>.immediateError(error, stackTrace); | 36 return new _FutureImpl<T>.immediateError(error, stackTrace); |
| 37 } | 37 } |
| 38 | 38 |
| 39 factory Future.delayed(int milliseconds, dynamic value()) { | |
| 40 var completer = new Completer<T>(); | |
| 41 new Timer(milliseconds, (_) => completer.complete(null)); | |
| 42 return completer.future.then((_) => value()); | |
| 43 } | |
| 44 | |
| 39 // TODO(floitsch): I don't think the typing is right here. | 45 // TODO(floitsch): I don't think the typing is right here. |
| 40 // Otherwise new Future<int>.wait(...) would be a Future<List<int>>. Sounds | 46 // Otherwise new Future<int>.wait(...) would be a Future<List<int>>. Sounds |
| 41 // wrong. | 47 // wrong. |
| 42 factory Future.wait(List<Future> futures) | 48 factory Future.wait(List<Future> futures) |
| 43 => new _FutureImpl<List<T>>.wait(futures); | 49 => new _FutureImpl<List<T>>.wait(futures); |
| 44 | 50 |
| 45 factory Future.delayed(int milliseconds, dynamic value()) { | |
| 46 var completer = new Completer<T>(); | |
| 47 new Timer(milliseconds, (_) => completer.complete(null)); | |
| 48 return completer.future.then((_) => value()); | |
| 49 } | |
| 50 | |
| 51 /** | 51 /** |
| 52 * When this future completes with a value, then [onValue] is called with this | 52 * When this future completes with a value, then [onValue] is called with this |
| 53 * value. If [this] future is already completed then the invocation of | 53 * value. If [this] future is already completed then the invocation of |
| 54 * [onValue] is delayed until the next event-loop iteration. | 54 * [onValue] is delayed until the next event-loop iteration. |
| 55 * | 55 * |
| 56 * Returns a new [Future] [:f:]. | 56 * Returns a new [Future] [:f:]. |
| 57 * | 57 * |
| 58 * If [this] is completed with an error then [:f:] is completed with the same | 58 * If [this] is completed with an error then [:f:] is completed with the same |
| 59 * error. If [this] is completed with a value, then [:f:]'s completion value | 59 * error. If [this] is completed with a value, then [:f:]'s completion value |
| 60 * depends on the result of invoking [onValue] with [this]' completion value. | 60 * depends on the result of invoking [onValue] with [this]' completion value. |
| 61 * | 61 * |
| 62 * If [onValue] returns a [Future] [:f2:] then [:f:] and [:f2:] are chained. | 62 * If [onValue] returns a [Future] [:f2:] then [:f:] and [:f2:] are chained. |
| 63 * That is, [:f:] is completed with the completion value of [:f2:]. | 63 * That is, [:f:] is completed with the completion value of [:f2:]. |
| 64 * | 64 * |
| 65 * Otherwise [:f:] is completed with the return value of [onValue]. | 65 * Otherwise [:f:] is completed with the return value of [onValue]. |
| 66 * | 66 * |
| 67 * If [onValue] throws an exception, the returned future will receive the | 67 * If [onValue] throws an exception, the returned future will receive the |
| 68 * exception. | 68 * exception. If the value thrown is an [AsyncError], it is used directly, |
| 69 * as the error result, otherwise it's wrapped in an [AsyncError] first. | |
|
floitsch
2013/01/10 16:38:10
it is
Lasse Reichstein Nielsen
2013/01/11 07:02:05
Done.
| |
| 69 * | 70 * |
| 70 * If [onError] is provided, it is called if this future completes with an | 71 * If [onError] is provided, it is called if this future completes with an |
| 71 * error, and its return value/throw behavior is handled the same way as | 72 * error, and its return value/throw behavior is handled the same way as |
| 72 * for [onValue]. | 73 * for [catchError] without a [:test:] argument. |
| 73 * | 74 * |
| 74 * In most cases, it is more readable to use [catchError] separately, possibly | 75 * In most cases, it is more readable to use [catchError] separately, possibly |
| 75 * with a [:test:] parameter, instead of handling both value and error in a | 76 * with a [:test:] parameter, instead of handling both value and error in a |
| 76 * single [then] call. | 77 * single [then] call. |
| 77 */ | 78 */ |
| 78 Future then(onValue(T value), { onError(AsyncError asyncError) }); | 79 Future then(onValue(T value), { onError(AsyncError asyncError) }); |
| 79 | 80 |
| 80 /** | 81 /** |
| 81 * If this future is complete with an error, [test] is called with the error. | 82 * Handles errors emitted by this [Future]. |
| 83 * | |
| 84 * When this future completes with an error, first [test] is called with the | |
| 85 * error's value. | |
| 86 * | |
| 82 * If [test] returns [true], [onError] is called with the error | 87 * If [test] returns [true], [onError] is called with the error |
| 83 * wrapped in an [AsyncError]. The result of [onError] is handled exactly as | 88 * wrapped in an [AsyncError]. The result of [onError] is handled exactly as |
| 84 * [then]'s [onValue]. If [test] returns false, the exception is not handled | 89 * [then]'s [onValue]. |
| 85 * by [onError]. If [test] is omitted, it defaults to a function that always | 90 * |
| 86 * returns true. | 91 * If [test] returns false, the exception is not handled by [onError], but is |
| 92 * emitted by the returned Future unmodified. | |
|
floitsch
2013/01/10 16:38:10
In theory it is, as if the handler was a 'then'-ha
Lasse Reichstein Nielsen
2013/01/11 07:02:05
Not really, no.
| |
| 93 * | |
| 94 * If [test] is omitted, it defaults to a function that always returns true. | |
| 87 * | 95 * |
| 88 * Example: | 96 * Example: |
| 89 * foo | 97 * foo |
| 90 * .catchError(..., test: (e) => e is ArgumentError) | 98 * .catchError(..., test: (e) => e is ArgumentError) |
| 91 * .catchError(..., test: (e) => e is NoSuchMethodError) | 99 * .catchError(..., test: (e) => e is NoSuchMethodError) |
| 92 * .then((v) { ... }); | 100 * .then((v) { ... }); |
| 93 */ | 101 */ |
| 94 Future catchError(onError(AsyncError asyncError), | 102 Future catchError(onError(AsyncError asyncError), |
| 95 {bool test(Object error)}); | 103 {bool test(Object error)}); |
| 96 | 104 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 * All listeners on the future will be immediately informed about the value. | 164 * All listeners on the future will be immediately informed about the value. |
| 157 */ | 165 */ |
| 158 void complete([T value]); | 166 void complete([T value]); |
| 159 | 167 |
| 160 /** | 168 /** |
| 161 * Complete [future] with an error. | 169 * Complete [future] with an error. |
| 162 * | 170 * |
| 163 * Completing a future with an error indicates that an exception was thrown | 171 * Completing a future with an error indicates that an exception was thrown |
| 164 * while trying to produce a value. | 172 * while trying to produce a value. |
| 165 * | 173 * |
| 166 * The argument [exception] should not be [:null:]. A [stackTrace] | 174 * The argument [exception] should not be [:null:]. |
| 167 * object can be provided as well, to give the user information about where | 175 * |
| 168 * the error occurred. If omitted, it will be [:null:]. | 176 * If [exception] is an [AsyncError], it is used directly as the error |
| 177 * message sent to the future's listeners, and [stackTrace] is ignored. | |
| 178 * | |
| 179 * Otherwise the [exception] and an optional [stackTrace] is combined and sent | |
|
floitsch
2013/01/10 16:38:10
.. is combined into an [AsyncError] and ...
Lasse Reichstein Nielsen
2013/01/11 07:02:05
Done.
| |
| 180 * this future's listeners. | |
| 169 */ | 181 */ |
| 170 void completeError(Object exception, [Object stackTrace]); | 182 void completeError(Object exception, [Object stackTrace]); |
| 171 } | 183 } |
| 172 | 184 |
| 173 class Futures { | 185 class Futures { |
| 174 /** | 186 /** |
| 175 * Returns a future which will complete once all the futures in a list are | 187 * Returns a future which will complete once all the futures in a list are |
| 176 * complete. If any of the futures in the list completes with an exception, | 188 * complete. If any of the futures in the list completes with an exception, |
| 177 * the resulting future also completes with an exception. (The value of the | 189 * the resulting future also completes with an exception. (The value of the |
| 178 * returned future will be a list of all the values that were produced.) | 190 * returned future will be a list of all the values that were produced.) |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 191 */ | 203 */ |
| 192 static Future forEach(Iterable input, Future f(element)) { | 204 static Future forEach(Iterable input, Future f(element)) { |
| 193 var iterator = input.iterator; | 205 var iterator = input.iterator; |
| 194 Future nextElement(_) { | 206 Future nextElement(_) { |
| 195 if (!iterator.moveNext()) return new Future.immediate(null); | 207 if (!iterator.moveNext()) return new Future.immediate(null); |
| 196 return f(iterator.current).then(nextElement); | 208 return f(iterator.current).then(nextElement); |
| 197 } | 209 } |
| 198 return nextElement(null); | 210 return nextElement(null); |
| 199 } | 211 } |
| 200 } | 212 } |
| OLD | NEW |