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

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

Issue 1296623004: Accept a computation that returns a Future for Future.delayed. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Address comments. Created 5 years, 4 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
« no previous file with comments | « no previous file | tests/lib/async/future_constructor2_test.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 * An object representing a delayed computation. 8 * An object representing a delayed computation.
9 * 9 *
10 * A [Future] is used to represent a potential value, or error, 10 * A [Future] is used to represent a potential value, or error,
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 * If [computation] is omitted, 214 * If [computation] is omitted,
215 * it will be treated as if [computation] was set to `() => null`, 215 * it will be treated as if [computation] was set to `() => null`,
216 * and the future will eventually complete with the `null` value. 216 * and the future will eventually complete with the `null` value.
217 * 217 *
218 * If calling [computation] throws, the created future will complete with the 218 * If calling [computation] throws, the created future will complete with the
219 * error. 219 * error.
220 * 220 *
221 * See also [Completer] for a way to create and complete a future at a 221 * See also [Completer] for a way to create and complete a future at a
222 * later time that isn't necessarily after a known fixed duration. 222 * later time that isn't necessarily after a known fixed duration.
223 */ 223 */
224 factory Future.delayed(Duration duration, [T computation()]) { 224 factory Future.delayed(Duration duration, [computation()]) {
225 _Future result = new _Future<T>(); 225 _Future result = new _Future<T>();
226 new Timer(duration, () { 226 new Timer(duration, () {
227 try { 227 try {
228 result._complete(computation == null ? null : computation()); 228 result._complete(computation == null ? null : computation());
229 } catch (e, s) { 229 } catch (e, s) {
230 _completeWithErrorCallback(result, e, s); 230 _completeWithErrorCallback(result, e, s);
231 } 231 }
232 }); 232 });
233 return result; 233 return result;
234 } 234 }
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 if (replacement != null) { 720 if (replacement != null) {
721 error = _nonNullError(replacement.error); 721 error = _nonNullError(replacement.error);
722 stackTrace = replacement.stackTrace; 722 stackTrace = replacement.stackTrace;
723 } 723 }
724 result._completeError(error, stackTrace); 724 result._completeError(error, stackTrace);
725 } 725 }
726 726
727 /** Helper function that converts `null` to a [NullThrownError]. */ 727 /** Helper function that converts `null` to a [NullThrownError]. */
728 Object _nonNullError(Object error) => 728 Object _nonNullError(Object error) =>
729 (error != null) ? error : new NullThrownError(); 729 (error != null) ? error : new NullThrownError();
OLDNEW
« no previous file with comments | « no previous file | tests/lib/async/future_constructor2_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698