OLD | NEW |
---|---|
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 // Dart core library. | 5 // Dart core library. |
6 | 6 |
7 | 7 |
8 /** | 8 /** |
9 * A [Future] is used to obtain a value sometime in the future. Receivers of a | 9 * A [Future] is used to obtain a value sometime in the future. Receivers of a |
10 * [Future] obtain the value by passing a callback to [then]. For example: | 10 * [Future] obtain the value by passing a callback to [then]. For example: |
(...skipping 24 matching lines...) Expand all Loading... | |
35 */ | 35 */ |
36 bool get isComplete(); | 36 bool get isComplete(); |
37 | 37 |
38 /** | 38 /** |
39 * Whether the value is available (meaning [isComplete] is true, and there was | 39 * Whether the value is available (meaning [isComplete] is true, and there was |
40 * no exception). | 40 * no exception). |
41 */ | 41 */ |
42 bool get hasValue(); | 42 bool get hasValue(); |
43 | 43 |
44 /** | 44 /** |
45 * When this future is complete and has a value, then [onComplete] is called | 45 * When this future is complete (either with a value or with an exception), |
Jennifer Messerly
2012/06/04 20:11:17
It'd be good to comment on the order that onComple
sam.mccall
2012/06/04 21:35:57
Agreed, I'll add this to the class doc.
| |
46 * then [complete] is called with the future. | |
47 * If [complete] throws an exception, it is ignored. | |
48 */ | |
49 void onComplete(void complete(Future<T> future)); | |
Bob Nystrom
2012/06/04 20:20:40
How about "always"?
sam.mccall
2012/06/04 21:35:57
My objection to 'always' is that it mostly makes s
Jennifer Messerly
2012/06/04 22:12:49
yeah. FWIW, I liked the name onComplete
| |
50 | |
51 /** | |
52 * If this future is complete and has a value, then [onValue] is called | |
46 * with the value. | 53 * with the value. |
47 */ | 54 */ |
48 void then(void onComplete(T value)); | 55 void then(void onValue(T value)); |
49 | 56 |
50 /** | 57 /** |
51 * If this future gets an exception, then call [onException]. | 58 * If this future is complete and has an exception, then call [onException]. |
52 * | 59 * |
53 * If [onException] returns true, then the exception is considered handled. | 60 * If [onException] returns true, then the exception is considered handled. |
54 * | 61 * |
55 * If [onException] does not return true (or [handleException] was never | 62 * If [onException] does not return true (or [handleException] was never |
56 * called), then the exception is not considered handled. In that case, if | 63 * called), then the exception is not considered handled. In that case, if |
57 * there were any calls to [then], then the exception will be thrown when the | 64 * there were any calls to [then], then the exception will be thrown when the |
58 * value is set. | 65 * value is set. |
59 * | 66 * |
60 * In most cases it should not be necessary to call [handleException], | 67 * In most cases it should not be necessary to call [handleException], |
61 * because the exception associated with this [Future] will propagate | 68 * because the exception associated with this [Future] will propagate |
62 * naturally if the future's value is being consumed. Only call | 69 * naturally if the future's value is being consumed. Only call |
63 * [handleException] if you need to do some special local exception handling | 70 * [handleException] if you need to do some special local exception handling |
64 * related to this particular Future's value. | 71 * related to this particular Future's value. |
65 */ | 72 */ |
66 void handleException(bool onException(Object exception)); | 73 void handleException(bool onException(Object exception)); |
67 | 74 |
68 /** | 75 /** |
69 * A future representing [transformation] applied to this future's value. | 76 * A future representing [transformation] applied to this future's value. |
70 * | 77 * |
71 * When this future gets a value, [transformation] will be called on the | 78 * When this future gets a value, [transformation] will be called on the |
72 * value, and the returned future will receive the result. | 79 * value, and the returned future will receive the result. |
73 * | 80 * |
74 * If an exception occurs (received by this future, or thrown by | 81 * If an exception occurs (received by this future, or thrown by |
75 * [transformation]) then the returned future will receive the exception. | 82 * [transformation]) then the returned future will receive the exception. |
76 * | |
77 * You must not add exception handlers to [this] future prior to calling | |
78 * transform, and any you add afterwards will not be invoked. | |
79 */ | 83 */ |
80 Future transform(transformation(T value)); | 84 Future transform(transformation(T value)); |
81 | 85 |
82 /** | 86 /** |
83 * A future representing an asynchronous transformation applied to this | 87 * A future representing an asynchronous transformation applied to this |
84 * future's value. [transformation] must return a Future. | 88 * future's value. [transformation] must return a Future. |
85 * | 89 * |
86 * When this future gets a value, [transformation] will be called on the | 90 * When this future gets a value, [transformation] will be called on the |
87 * value. When the resulting future gets a value, the returned future | 91 * value. When the resulting future gets a value, the returned future |
88 * will receive it. | 92 * will receive it. |
89 * | 93 * |
90 * If an exception occurs (received by this future, thrown by | 94 * If an exception occurs (received by this future, thrown by |
91 * [transformation], or received by the future returned by [transformation]) | 95 * [transformation], or received by the future returned by [transformation]) |
92 * then the returned future will receive the exception. | 96 * then the returned future will receive the exception. |
93 * | |
94 * You must not add exception handlers to [this] future prior to calling | |
95 * chain, and any you add afterwards will not be invoked. | |
96 */ | 97 */ |
97 Future chain(Future transformation(T value)); | 98 Future chain(Future transformation(T value)); |
98 } | 99 } |
99 | 100 |
100 | 101 |
101 /** | 102 /** |
102 * A [Completer] is used to produce [Future]s and supply their value when it | 103 * A [Completer] is used to produce [Future]s and supply their value when it |
103 * becomes available. | 104 * becomes available. |
104 * | 105 * |
105 * A service that provides values to callers, and wants to return [Future]s can | 106 * A service that provides values to callers, and wants to return [Future]s can |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 } | 188 } |
188 }); | 189 }); |
189 future.handleException((exception) { | 190 future.handleException((exception) { |
190 if (!result.isComplete) completer.completeException(exception); | 191 if (!result.isComplete) completer.completeException(exception); |
191 return true; | 192 return true; |
192 }); | 193 }); |
193 } | 194 } |
194 return result; | 195 return result; |
195 } | 196 } |
196 } | 197 } |
OLD | NEW |