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

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: WIth tests and bugfixes 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 | tests/lib/async/future_test.dart » ('j') | tests/lib/async/future_test.dart » ('J')
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 used to return
91 * a completed future with that result.
92 * If [function] returns a [Future], this will create a future that
93 * will eventually complete with the same result.
floitsch 2013/02/01 12:44:11 Maybe explain it in contrast to "immediate". This
Lasse Reichstein Nielsen 2013/02/01 13:16:50 I think I'll do it the opposite way: new Future.i
94 */
95 factory Future.of(function()) {
96 try {
97 var result = function();
98 if (result is Future) return new _FutureWrapper<T>(result);
floitsch 2013/02/01 12:44:11 should we wrap?
Lasse Reichstein Nielsen 2013/02/01 13:16:50 That was my question too. I decided to do it just
99 return new _FutureImpl<T>.immediate(result);
100 } catch (error, stackTrace) {
101 return new _FutureImpl<T>.immediateError(error, stackTrace);
102 }
103 }
104
105 /**
88 * A future whose value is available in the next event-loop iteration. 106 * A future whose value is available in the next event-loop iteration.
89 * 107 *
90 * See [Completer]s, for futures with values that are computed asynchronously. 108 * See [Completer]s, for futures with values that are computed asynchronously.
91 */ 109 */
92 factory Future.immediate(T value) => new _FutureImpl<T>.immediate(value); 110 factory Future.immediate(T value) => new _FutureImpl<T>.immediate(value);
93 111
94 /** 112 /**
95 * A future that completes with an error in the next event-loop iteration. 113 * A future that completes with an error in the next event-loop iteration.
96 * 114 *
97 * See [Completer]s, for futures with values that are computed asynchronously. 115 * See [Completer]s, for futures with values that are computed asynchronously.
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 * The argument [exception] should not be `null`. 326 * The argument [exception] should not be `null`.
309 * 327 *
310 * If [exception] is an [AsyncError], it is used directly as the error 328 * If [exception] is an [AsyncError], it is used directly as the error
311 * message sent to the future's listeners, and [stackTrace] is ignored. 329 * message sent to the future's listeners, and [stackTrace] is ignored.
312 * 330 *
313 * Otherwise the [exception] and an optional [stackTrace] is combined into an 331 * Otherwise the [exception] and an optional [stackTrace] is combined into an
314 * [AsyncError] and sent to this future's listeners. 332 * [AsyncError] and sent to this future's listeners.
315 */ 333 */
316 void completeError(Object exception, [Object stackTrace]); 334 void completeError(Object exception, [Object stackTrace]);
317 } 335 }
OLDNEW
« no previous file with comments | « no previous file | tests/lib/async/future_test.dart » ('j') | tests/lib/async/future_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698