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

Side by Side Diff: corelib/src/future.dart

Issue 8271014: future and completer (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 2 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 | « no previous file | corelib/src/implementation/future_implementation.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 // Dart core library.
6
7
8 /**
9 * A Future is used to obtain a value sometime in the
10 * future.
11 *
12 * Receivers of a Future obtain the value by passing
13 * a callback to the 'then' method of Future.
14 *
15 * For example:
16 *
17 * Future<int> future = getFutureFromSomewhere();
18 * future.then((value) {
19 * print("I received the number " + value);
20 * });
21 *
22 */
23 interface Future<T> factory FutureImpl<T> {
24
25 /**
26 * The value this future provided. (If called when hasValue
27 * is false, then throws an exception.)
28 */
29 T get value();
30
31 /**
32 * Exception that occurred (null if no exception occured). (If called
33 * before [isComplete] is true, then this exception property itself
34 * throws a FutureNotCompleteException.)
35 */
36 Object get exception();
37
38 /**
39 * Whether the future is complete (either the value is available or there
40 * was an exception).
41 */
42 bool get isComplete();
43
44 /**
45 * Whether the value is available (meaning isComplete is true, and there
46 * was no exception).
47 */
48 bool get hasValue();
49
50 /**
51 * When this future is complete and has a value, then call
52 * the onComplete callback function with the value.
53 */
54 void then(void onComplete(T value));
55
56 /**
57 * If this future gets an exception, then call onException.
58 *
59 * If onException returns true, then the exception is considered
60 * handled.
61 *
62 * If onException does not return true (or handleException was never called),
63 * then the exception is not considered handled. In that case, if there were
64 * any calls to [then] (meaning that there are onComplete callbacks waiting
65 * for the value), then the exception will be thrown when it is set.
66 *
67 * (In most cases it should not be necessary to call handleException,
68 * because the exception associated with this Future will propagate naturally
69 * if the future's value is being consumed. Only call handleException if you
70 * need to do some special local exception handling related to this
71 * particular Future's value.)
72 */
73 void handleException(bool onException(Object exception));
Siggi Cherem (dart-lang) 2011/10/13 20:45:44 Just to document here our discussion, even though
74 }
75
76
77 /**
78 * A Completer is used to produce Future objects, and supply
79 * a value to the Future object when the value becomes available.
80 *
81 * A service that provides values to callers, and wants to return Future objects
82 * rather than returning the values immediately, can use a Completer as follows:
83 *
84 * Completer completer = new Completer();
85 * Future future = completer.future;
86 *
87 * // send [future] object back to client...
88 *
89 * // later when value is available, call:
90 * completer.setValue(value);
91 *
92 * // alternatively, if the service cannot produce the value, it
93 * // can set an exception:
94 * completer.setException(exception);
95 *
96 * // alternatively, if the service obtains an object that
97 * // is usually the value, but might be an Exception, then
98 * // it can call:
99 * completer.complete(valueOrException);
100 *
101 */
102 interface Completer<T> factory CompleterImpl<T> {
Siggi Cherem (dart-lang) 2011/10/13 20:45:44 Here is the simplified interface we discussed this
103
104 /** Create a completer */
105 Completer();
106
107 Future get future();
108
109 /**
110 * Called when value is available.
111 */
112 void setValue(T value);
113
114 /**
115 * Called if an exception occured while trying to produce value.
116 */
117 void setException(Object exception);
118
119 /**
120 * Convenience method to call setValue or setException as appropriate.
121 * If valueOrException is an Exception, then this calls setException.
122 * If valueOrException is not an Exception, then this calls setValue.
123 */
124 void complete(var valueOrException);
125 }
OLDNEW
« no previous file with comments | « no previous file | corelib/src/implementation/future_implementation.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698