Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(854)

Side by Side Diff: lib/core/future.dart

Issue 10948009: Revert 12494. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « lib/core/expect.dart ('k') | lib/core/num.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 /** 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) {
11 * print("I received the number $value"); 11 * print("I received the number $value");
12 * }); 12 * });
13 * 13 *
14 * A future may complete by *succeeding* (producing a value) or *failing* 14 * A future may complete by *succeeding* (producing a value) or *failing*
15 * (producing an exception, which may be handled with [handleException]). 15 * (producing an exception, which may be handled with [handleException]).
16 * Callbacks passed to [onComplete] will be invoked in either case. 16 * Callbacks passed to [onComplete] will be invoked in either case.
17 * 17 *
18 * When a future completes, the following actions happen in order: 18 * When a future completes, the following actions happen in order:
19 * 19 *
20 * 1. if the future suceeded, handlers registered with [then] are called. 20 * 1. if the future suceeded, handlers registered with [then] are called.
21 * 2. if the future failed, handlers registered with [handleException] are 21 * 2. if the future failed, handlers registered with [handleException] are
22 * called in sequence, until one returns true. 22 * called in sequence, until one returns true.
23 * 3. handlers registered with [onComplete] are called 23 * 3. handlers registered with [onComplete] are called
24 * 4. if the future failed, and at least one handler was registered with 24 * 4. if the future failed, and at least one handler was registered with
25 * [then], and no handler registered with [handleException] returned 25 * [then], and no handler registered with [handleException] returned
26 * [:true:], then the exception is thrown. 26 * [:true:], then the exception is thrown.
27 * 27 *
28 * Use a [Completer] to create and change the state of a [Future]. 28 * Use a [Completer] to create and change the state of a [Future].
29 */ 29 */
30 abstract class Future<T> { 30 interface Future<T> default FutureImpl<T> {
31
31 /** A future whose value is immediately available. */ 32 /** A future whose value is immediately available. */
32 factory Future.immediate(T value) => new FutureImpl<T>.immediate(value); 33 Future.immediate(T value);
33 34
34 /** The value provided. Throws an exception if [hasValue] is false. */ 35 /** The value provided. Throws an exception if [hasValue] is false. */
35 T get value; 36 T get value;
36 37
37 /** 38 /**
38 * Exception that occurred ([:null:] if no exception occured). This property 39 * Exception that occurred ([:null:] if no exception occured). This property
39 * throws a [FutureNotCompleteException] if it is used before this future is 40 * throws a [FutureNotCompleteException] if it is used before this future is
40 * completes. 41 * completes.
41 */ 42 */
42 Object get exception; 43 Object get exception;
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 * ... 151 * ...
151 * 152 *
152 * // later when value is available, call: 153 * // later when value is available, call:
153 * completer.complete(value); 154 * completer.complete(value);
154 * 155 *
155 * // alternatively, if the service cannot produce the value, it 156 * // alternatively, if the service cannot produce the value, it
156 * // can provide an exception: 157 * // can provide an exception:
157 * completer.completeException(exception); 158 * completer.completeException(exception);
158 * 159 *
159 */ 160 */
160 abstract class Completer<T> { 161 interface Completer<T> default CompleterImpl<T> {
161 162
162 factory Completer() => new CompleterImpl<T>(); 163 Completer();
163 164
164 /** The future that will contain the value produced by this completer. */ 165 /** The future that will contain the value produced by this completer. */
165 Future get future; 166 Future get future;
166 167
167 /** Supply a value for [future]. */ 168 /** Supply a value for [future]. */
168 void complete(T value); 169 void complete(T value);
169 170
170 /** 171 /**
171 * Indicate in [future] that an exception occured while trying to produce its 172 * Indicate in [future] that an exception occured while trying to produce its
172 * value. The argument [exception] should not be [:null:]. A [stackTrace] 173 * value. The argument [exception] should not be [:null:]. A [stackTrace]
(...skipping 17 matching lines...) Expand all
190 FutureAlreadyCompleteException() {} 191 FutureAlreadyCompleteException() {}
191 String toString() => "Exception: future already completed"; 192 String toString() => "Exception: future already completed";
192 } 193 }
193 194
194 195
195 /** 196 /**
196 * [Futures] holds additional utility functions that operate on [Future]s (for 197 * [Futures] holds additional utility functions that operate on [Future]s (for
197 * example, waiting for a collection of Futures to complete). 198 * example, waiting for a collection of Futures to complete).
198 */ 199 */
199 class Futures { 200 class Futures {
201
200 /** 202 /**
201 * Returns a future which will complete once all the futures in a list are 203 * Returns a future which will complete once all the futures in a list are
202 * complete. If any of the futures in the list completes with an exception, 204 * complete. If any of the futures in the list completes with an exception,
203 * the resulting future also completes with an exception. (The value of the 205 * the resulting future also completes with an exception. (The value of the
204 * returned future will be a list of all the values that were produced.) 206 * returned future will be a list of all the values that were produced.)
205 */ 207 */
206 static Future<List> wait(List<Future> futures) { 208 static Future<List> wait(List<Future> futures) {
207 if (futures.isEmpty()) { 209 if (futures.isEmpty()) {
208 return new Future<List>.immediate(const []); 210 return new Future<List>.immediate(const []);
209 } 211 }
(...skipping 19 matching lines...) Expand all
229 future.handleException((exception) { 231 future.handleException((exception) {
230 if (!result.isComplete) { 232 if (!result.isComplete) {
231 completer.completeException(exception, future.stackTrace); 233 completer.completeException(exception, future.stackTrace);
232 } 234 }
233 return true; 235 return true;
234 }); 236 });
235 } 237 }
236 return result; 238 return result;
237 } 239 }
238 } 240 }
OLDNEW
« no previous file with comments | « lib/core/expect.dart ('k') | lib/core/num.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698