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] can obtain the value by passing a callback to [then]. |
11 * For example: | |
11 * | 12 * |
12 * Future<int> future = getFutureFromSomewhere(); | 13 * Future<int> future = getFutureFromSomewhere(); |
13 * future.then((value) { | 14 * future.then((value) { |
14 * print("I received the number $value"); | 15 * print("I received the number $value"); |
15 * }); | 16 * }); |
17 * | |
18 * A future may complete by *succeeding* (producing a value) or *failing* | |
19 * (producing an exception, which may be handled with [handleException]). | |
20 * Callbacks passed to [onComplete] will be invoked in either case. | |
21 * | |
22 * When a future completes: | |
Siggi Cherem (dart-lang)
2012/06/06 20:42:49
say in order somewhere? For instance,
=> When a fu
| |
23 * | |
24 * 1. if the future suceeded, handlers registered with [then] are called. | |
25 * 2. if the future failed, handlers registered with [handleException] are | |
26 * called in sequence, until one returns true. | |
27 * 3. handlers registered with [onComplete] are called | |
28 * 4. if the future failed, and at least one handler was registered with | |
29 * [then], and no handler registered with [handleException] returned | |
30 * [:true:], then the exception is thrown. | |
16 */ | 31 */ |
17 interface Future<T> default FutureImpl<T> { | 32 interface Future<T> default FutureImpl<T> { |
18 | 33 |
19 /** A future whose value is immediately available. */ | 34 /** A future whose value is immediately available. */ |
20 Future.immediate(T value); | 35 Future.immediate(T value); |
21 | 36 |
22 /** The value provided. Throws an exception if [hasValue] is false. */ | 37 /** The value provided. Throws an exception if [hasValue] is false. */ |
23 T get value(); | 38 T get value(); |
24 | 39 |
25 /** | 40 /** |
26 * Exception that occurred ([:null:] if no exception occured). This property | 41 * Exception that occurred ([:null:] if no exception occured). This property |
27 * throws a [FutureNotCompleteException] if it is used before this future is | 42 * throws a [FutureNotCompleteException] if it is used before this future is |
28 * completes. | 43 * completes. |
29 */ | 44 */ |
30 Object get exception(); | 45 Object get exception(); |
31 | 46 |
32 /** | 47 /** |
33 * Whether the future is complete (either the value is available or there was | 48 * Whether the future is complete (either the value is available or there was |
34 * an exception). | 49 * an exception). |
35 */ | 50 */ |
36 bool get isComplete(); | 51 bool get isComplete(); |
37 | 52 |
38 /** | 53 /** |
39 * Whether the value is available (meaning [isComplete] is true, and there was | 54 * Whether the value is available (meaning [isComplete] is true, and there was |
40 * no exception). | 55 * no exception). |
41 */ | 56 */ |
42 bool get hasValue(); | 57 bool get hasValue(); |
43 | 58 |
44 /** | 59 /** |
45 * When this future is complete and has a value, then [onComplete] is called | 60 * When this future is complete (either with a value or with an exception), |
61 * then [complete] is called with the future. | |
62 * If [complete] throws an exception, it is ignored. | |
63 */ | |
64 void onComplete(void complete(Future<T> future)); | |
65 | |
66 /** | |
67 * If this future is complete and has a value, then [onValue] is called | |
46 * with the value. | 68 * with the value. |
47 */ | 69 */ |
48 void then(void onComplete(T value)); | 70 void then(void onValue(T value)); |
49 | 71 |
50 /** | 72 /** |
51 * If this future gets an exception, then call [onException]. | 73 * If this future is complete and has an exception, then call [onException]. |
52 * | 74 * |
53 * If [onException] returns true, then the exception is considered handled. | 75 * If [onException] returns true, then the exception is considered handled. |
54 * | 76 * |
55 * If [onException] does not return true (or [handleException] was never | 77 * If [onException] does not return true (or [handleException] was never |
56 * called), then the exception is not considered handled. In that case, if | 78 * 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 | 79 * there were any calls to [then], then the exception will be thrown when the |
58 * value is set. | 80 * value is set. |
59 * | 81 * |
60 * In most cases it should not be necessary to call [handleException], | 82 * In most cases it should not be necessary to call [handleException], |
61 * because the exception associated with this [Future] will propagate | 83 * because the exception associated with this [Future] will propagate |
62 * naturally if the future's value is being consumed. Only call | 84 * naturally if the future's value is being consumed. Only call |
63 * [handleException] if you need to do some special local exception handling | 85 * [handleException] if you need to do some special local exception handling |
64 * related to this particular Future's value. | 86 * related to this particular Future's value. |
65 */ | 87 */ |
66 void handleException(bool onException(Object exception)); | 88 void handleException(bool onException(Object exception)); |
67 | 89 |
68 /** | 90 /** |
69 * A future representing [transformation] applied to this future's value. | 91 * A future representing [transformation] applied to this future's value. |
70 * | 92 * |
71 * When this future gets a value, [transformation] will be called on the | 93 * When this future gets a value, [transformation] will be called on the |
72 * value, and the returned future will receive the result. | 94 * value, and the returned future will receive the result. |
73 * | 95 * |
74 * If an exception occurs (received by this future, or thrown by | 96 * If an exception occurs (received by this future, or thrown by |
75 * [transformation]) then the returned future will receive the exception. | 97 * [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 */ | 98 */ |
80 Future transform(transformation(T value)); | 99 Future transform(transformation(T value)); |
81 | 100 |
82 /** | 101 /** |
83 * A future representing an asynchronous transformation applied to this | 102 * A future representing an asynchronous transformation applied to this |
84 * future's value. [transformation] must return a Future. | 103 * future's value. [transformation] must return a Future. |
85 * | 104 * |
86 * When this future gets a value, [transformation] will be called on the | 105 * When this future gets a value, [transformation] will be called on the |
87 * value. When the resulting future gets a value, the returned future | 106 * value. When the resulting future gets a value, the returned future |
88 * will receive it. | 107 * will receive it. |
89 * | 108 * |
90 * If an exception occurs (received by this future, thrown by | 109 * If an exception occurs (received by this future, thrown by |
91 * [transformation], or received by the future returned by [transformation]) | 110 * [transformation], or received by the future returned by [transformation]) |
92 * then the returned future will receive the exception. | 111 * 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 */ | 112 */ |
97 Future chain(Future transformation(T value)); | 113 Future chain(Future transformation(T value)); |
98 } | 114 } |
99 | 115 |
100 | 116 |
101 /** | 117 /** |
102 * A [Completer] is used to produce [Future]s and supply their value when it | 118 * A [Completer] is used to produce [Future]s and supply their value when it |
103 * becomes available. | 119 * becomes available. |
104 * | 120 * |
105 * A service that provides values to callers, and wants to return [Future]s can | 121 * 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 } | 203 } |
188 }); | 204 }); |
189 future.handleException((exception) { | 205 future.handleException((exception) { |
190 if (!result.isComplete) completer.completeException(exception); | 206 if (!result.isComplete) completer.completeException(exception); |
191 return true; | 207 return true; |
192 }); | 208 }); |
193 } | 209 } |
194 return result; | 210 return result; |
195 } | 211 } |
196 } | 212 } |
OLD | NEW |