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

Side by Side Diff: sdk/lib/async/future.dart

Issue 12091105: Add Future.of that calls a function and captures the result (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Better comment. Created 7 years, 10 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 | sdk/lib/async/future_impl.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 part of dart.async; 5 part of dart.async;
6 6
7 /** 7 /**
8 * A [Future] represents a delayed computation. It is used to obtain a not-yet 8 * A [Future] represents a delayed computation. It is used to obtain a not-yet
9 * available value, or error, sometime in the future. Receivers of a 9 * available value, or error, sometime in the future. Receivers of a
10 * [Future] can register callbacks that handle the value or error once it is 10 * [Future] can register callbacks that handle the value or error once it is
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 * [catchError]) is handling the errors for exceptions coming from calls to 78 * [catchError]) is handling the errors for exceptions coming from calls to
79 * 'foo', as well as 'bar'. This would not be the case if the error-handler was 79 * 'foo', as well as 'bar'. This would not be the case if the error-handler was
80 * registered at the same time as the value-handler. 80 * registered at the same time as the value-handler.
81 * 81 *
82 * Futures can have more than one callback-pairs registered. Each successor is 82 * Futures can have more than one callback-pairs registered. Each successor is
83 * treated independently and is handled as if it was the only successor. 83 * treated independently and is handled as if it was the only successor.
84 */ 84 */
85 // TODO(floitsch): document chaining. 85 // TODO(floitsch): document chaining.
86 abstract class Future<T> { 86 abstract class Future<T> {
87 /** 87 /**
88 * Creates a future containing the result of calling [function].
89 *
90 * The result of computing [:function():] is either a returned value or
91 * a throw.
92 *
93 * If a value is returned, it becomes the result of the created future.
94 *
95 * If calling [function] throws, the created [Future] will be completed
96 * with an async error containing the thrown value and a captured
97 * stacktrace.
98 *
99 * However, if the result of calling [function] is already an asynchronous
100 * result, we treat it specially.
101 *
102 * If the returned value is itself a [Future], completion of
103 * the created future will wait until the returned future completes,
104 * and will then complete with the same result.
105 *
106 * If a thrown value is an [AsyncError], it is used directly as the result
107 * of the created future.
108 */
109 factory Future.of(function()) {
110 try {
111 var result = function();
112 return new _FutureImpl<T>().._setOrChainValue(result);
113 } catch (error, stackTrace) {
114 return new _FutureImpl<T>.immediateError(error, stackTrace);
115 }
116 }
117
118 /**
88 * A future whose value is available in the next event-loop iteration. 119 * A future whose value is available in the next event-loop iteration.
89 * 120 *
90 * See [Completer]s, for futures with values that are computed asynchronously. 121 * If [value] is not a [Future], using this constructor is equivalent
122 * to [:new Future.of(() => value):].
123 *
124 * See [Completer] to create a Future and complete it later.
91 */ 125 */
92 factory Future.immediate(T value) => new _FutureImpl<T>.immediate(value); 126 factory Future.immediate(T value) => new _FutureImpl<T>.immediate(value);
93 127
94 /** 128 /**
95 * A future that completes with an error in the next event-loop iteration. 129 * A future that completes with an error in the next event-loop iteration.
96 * 130 *
97 * See [Completer]s, for futures with values that are computed asynchronously. 131 * See [Completer] to create a Future and complete it later.
98 */ 132 */
99 factory Future.immediateError(var error, [Object stackTrace]) { 133 factory Future.immediateError(var error, [Object stackTrace]) {
100 return new _FutureImpl<T>.immediateError(error, stackTrace); 134 return new _FutureImpl<T>.immediateError(error, stackTrace);
101 } 135 }
102 136
103 /** 137 /**
104 * Creates a future that completes after a delay. 138 * Creates a future that completes after a delay.
105 * 139 *
106 * The future will be completed after [milliseconds] have passed with 140 * The future will be completed after [milliseconds] have passed with
107 * the result of calling [value]. If [milliseconds] is 0, it completes at the 141 * the result of calling [value]. If [milliseconds] is 0, it completes at the
108 * earliest in the next event-loop iteration. 142 * earliest in the next event-loop iteration.
109 * 143 *
110 * If calling [value] throws, the created future will complete with the 144 * If calling [value] throws, the created future will complete with the
111 * error. 145 * error.
112 * 146 *
113 * See [Completer]s, for futures with values that are computed asynchronously. 147 * See [Completer]s, for futures with values that are computed asynchronously.
114 */ 148 */
115 factory Future.delayed(int milliseconds, T value()) { 149 factory Future.delayed(int milliseconds, T value()) {
116 _ThenFuture<dynamic, T> future = new _ThenFuture<dynamic, T>((_) => value()) ; 150 _ThenFuture<dynamic, T> future =
151 new _ThenFuture<dynamic, T>((_) => value());
117 new Timer(milliseconds, (_) => future._sendValue(null)); 152 new Timer(milliseconds, (_) => future._sendValue(null));
118 return future; 153 return future;
119 } 154 }
120 155
121 /** 156 /**
122 * Wait for all the given futures to complete and collect their values. 157 * Wait for all the given futures to complete and collect their values.
123 * 158 *
124 * Returns a future which will complete once all the futures in a list are 159 * Returns a future which will complete once all the futures in a list are
125 * complete. If any of the futures in the list completes with an error, 160 * complete. If any of the futures in the list completes with an error,
126 * the resulting future also completes with an error. Otherwise the value 161 * the resulting future also completes with an error. Otherwise the value
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 * The argument [exception] should not be `null`. 343 * The argument [exception] should not be `null`.
309 * 344 *
310 * If [exception] is an [AsyncError], it is used directly as the error 345 * If [exception] is an [AsyncError], it is used directly as the error
311 * message sent to the future's listeners, and [stackTrace] is ignored. 346 * message sent to the future's listeners, and [stackTrace] is ignored.
312 * 347 *
313 * Otherwise the [exception] and an optional [stackTrace] is combined into an 348 * Otherwise the [exception] and an optional [stackTrace] is combined into an
314 * [AsyncError] and sent to this future's listeners. 349 * [AsyncError] and sent to this future's listeners.
315 */ 350 */
316 void completeError(Object exception, [Object stackTrace]); 351 void completeError(Object exception, [Object stackTrace]);
317 } 352 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/async/future_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698