| 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 * An object representing a delayed computation. | 8 * An object representing a delayed computation. |
| 9 * | 9 * |
| 10 * A [Future] is used to obtain a not yet | 10 * A [Future] is used to obtain a not yet |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 * Futures can have more than one callback-pairs registered. Each successor is | 84 * Futures can have more than one callback-pairs registered. Each successor is |
| 85 * treated independently and is handled as if it was the only successor. | 85 * treated independently and is handled as if it was the only successor. |
| 86 */ | 86 */ |
| 87 // TODO(floitsch): document chaining. | 87 // TODO(floitsch): document chaining. |
| 88 abstract class Future<T> { | 88 abstract class Future<T> { |
| 89 | 89 |
| 90 /** | 90 /** |
| 91 * Creates a future containing the result of calling [computation] | 91 * Creates a future containing the result of calling [computation] |
| 92 * asynchronously with [Timer.run]. | 92 * asynchronously with [Timer.run]. |
| 93 * | 93 * |
| 94 * if the result of executing [computation] throws, the returned future is | 94 * If the result of executing [computation] throws, the returned future is |
| 95 * completed with the error. If a thrown value is an [AsyncError], it is used | 95 * completed with the error. |
| 96 * directly, instead of wrapping this error again in another [AsyncError]. | |
| 97 * | 96 * |
| 98 * If the returned value is itself a [Future], completion of | 97 * If the returned value is itself a [Future], completion of |
| 99 * the created future will wait until the returned future completes, | 98 * the created future will wait until the returned future completes, |
| 100 * and will then complete with the same result. | 99 * and will then complete with the same result. |
| 101 * | 100 * |
| 102 * If a value is returned, it becomes the result of the created future. | 101 * If a value is returned, it becomes the result of the created future. |
| 103 */ | 102 */ |
| 104 factory Future(computation()) { | 103 factory Future(computation()) { |
| 105 _Future result = new _Future<T>(); | 104 _Future result = new _Future<T>(); |
| 106 Timer.run(() { | 105 Timer.run(() { |
| 107 try { | 106 try { |
| 108 result._complete(computation()); | 107 result._complete(computation()); |
| 109 } catch (e, s) { | 108 } catch (e, s) { |
| 110 result._completeError(e, s); | 109 result._completeError(e, s); |
| 111 } | 110 } |
| 112 }); | 111 }); |
| 113 return result; | 112 return result; |
| 114 } | 113 } |
| 115 | 114 |
| 116 /** | 115 /** |
| 116 * Creates a future containing the result of calling [computation] |
| 117 * asynchronously with [scheduleMicrotask]. |
| 118 * |
| 119 * If the result of executing [computation] throws, the returned future is |
| 120 * completed with the error. |
| 121 * |
| 122 * If the returned value is itself a [Future], completion of |
| 123 * the created future will wait until the returned future completes, |
| 124 * and will then complete with the same result. |
| 125 * |
| 126 * If a value is returned, it becomes the result of the created future. |
| 127 */ |
| 128 factory Future.microtask(computation()) { |
| 129 _Future result = new _Future<T>(); |
| 130 scheduleMicrotask(() { |
| 131 try { |
| 132 result._complete(computation()); |
| 133 } catch (e, s) { |
| 134 result._completeError(e, s); |
| 135 } |
| 136 }); |
| 137 return result; |
| 138 } |
| 139 |
| 140 /** |
| 117 * Creates a future containing the result of immediately calling | 141 * Creates a future containing the result of immediately calling |
| 118 * [computation]. | 142 * [computation]. |
| 119 * | 143 * |
| 120 * If calling [computation] throws, the returned future is completed with the | 144 * If calling [computation] throws, the returned future is completed with the |
| 121 * error. If a thrown value is an [AsyncError], it is used | 145 * error. |
| 122 * directly, instead of wrapping this error again in another [AsyncError]. | |
| 123 * | 146 * |
| 124 * If the returned value is itself a [Future], completion of | 147 * If the returned value is itself a [Future], completion of |
| 125 * the created future will wait until the returned future completes, | 148 * the created future will wait until the returned future completes, |
| 126 * and will then complete with the same result. | 149 * and will then complete with the same result. |
| 127 */ | 150 */ |
| 128 factory Future.sync(computation()) { | 151 factory Future.sync(computation()) { |
| 129 try { | 152 try { |
| 130 var result = computation(); | 153 var result = computation(); |
| 131 return new Future<T>.value(result); | 154 return new Future<T>.value(result); |
| 132 } catch (error, stackTrace) { | 155 } catch (error, stackTrace) { |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 * | 499 * |
| 477 * The argument [exception] must not be `null`. | 500 * The argument [exception] must not be `null`. |
| 478 */ | 501 */ |
| 479 void completeError(Object exception, [Object stackTrace]); | 502 void completeError(Object exception, [Object stackTrace]); |
| 480 | 503 |
| 481 /** | 504 /** |
| 482 * Whether the future has been completed. | 505 * Whether the future has been completed. |
| 483 */ | 506 */ |
| 484 bool get isCompleted; | 507 bool get isCompleted; |
| 485 } | 508 } |
| OLD | NEW |