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

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

Issue 123743005: Update documentation of future.then/catchError. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 11 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 | no next file » | 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 obtain a not yet 10 * A [Future] is used to obtain a not yet
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 .then(nextElement, onError: doneSignal._completeError); 284 .then(nextElement, onError: doneSignal._completeError);
285 } else { 285 } else {
286 doneSignal._complete(null); 286 doneSignal._complete(null);
287 } 287 }
288 } 288 }
289 nextElement(null); 289 nextElement(null);
290 return doneSignal; 290 return doneSignal;
291 } 291 }
292 292
293 /** 293 /**
294 * When this future completes with a value, then [onValue] is called with this 294 * Register callbacks to be called when this future completes.
295 * value. If [this] future is already completed then the invocation of
296 * [onValue] is delayed until the next event-loop iteration.
297 * 295 *
298 * Returns a new [Future] `f` which is completed with the result of 296 * When this future completes with a value,
299 * invoking [onValue] (if [this] completes with a value) or [onError] (if 297 * the [onValue] callback will be called with that value.
300 * [this] completes with an error). 298 * If this future is already completed, the callback will not be called
299 * immediately, but will be scheduled in a later microtask.
301 * 300 *
302 * If the invoked callback throws an exception, the returned future `f` is 301 * If [onError] is provided, and this future completes with an error,
303 * completed with the error. 302 * the `onError` callback is called with that error its stack trace.
303 * The `onError` callback must have type `void onError(error)` or
Anders Johnsen 2014/01/06 11:15:23 void? Can't it return a value/future?
Lasse Reichstein Nielsen 2014/01/07 08:37:39 Good catch!
304 * `void onError(error, StackTrace stackTrace)`.
305 * If [onError] accepts two arguments,
306 * it is called with both the error and stack trace,
307 * otherwise it is called with just the error object.
304 * 308 *
305 * If the invoked callback returns a [Future] `f2` then `f` and `f2` are 309 * Returns a new [Future]
306 * chained. That is, `f` is completed with the completion value of `f2`. 310 * which is completed with the result of the call to `onValue`
311 * (if this future completes with a value)
312 * or to `onError` (if this future completes with an error).
307 * 313 *
308 * The [onError] callback must be of type `void onError(error)` or 314 * If the invoked callback throws an exception,
309 * `void onError(error, StackTrace stackTrace)`. If [onError] accepts 315 * the returned future is completed with the error
310 * two arguments it is called with the stack trace (which could be `null` if 316 * and a stack trace for the error.
311 * the stream itself received an error without stack trace). 317 * In the case of `onError`,
312 * Otherwise it is called with just the error object. 318 * if the exception thrown is the same as the argument to `onError`,
319 * the throw is considered a rethrow,
320 * and the original stack trace is used instead.
321 *
322 * If the callback returns a [Future],
323 * the future returned by `then` will be completed with
324 * the same result of the future returned by the callback.
313 * 325 *
314 * If [onError] is not given it forwards the error to `f`. 326 * If [onError] is not given it forwards the error to `f`.
315 * 327 *
316 * In most cases, it is more readable to use [catchError] separately, possibly 328 * In most cases, it is more readable to use [catchError] separately, possibly
317 * with a `test` parameter, instead of handling both value and error in a 329 * with a `test` parameter, instead of handling both value and error in a
318 * single [then] call. 330 * single [then] call.
319 */ 331 */
320 Future then(onValue(T value), { Function onError }); 332 Future then(onValue(T value), { Function onError });
321 333
322 /** 334 /**
323 * Handles errors emitted by this [Future]. 335 * Handles errors emitted by this [Future].
324 * 336 *
325 * Returns a new [Future] `f`. 337 * This is the asynchronous equivalent of a "catch" block.
326 * 338 *
327 * When [this] completes with a value, the value is forwarded to `f` 339 * Returns a new [Future] that will be completed with either the result of
328 * unmodified. That is, `f` completes with the same value. 340 * this future or the result of calling the `onError` callback.
329 * 341 *
330 * When [this] completes with an error, [test] is called with the 342 * If this future completes with a value,
331 * error's value. If the invocation returns [true], [onError] is called with 343 * the returned future completes with the same value.
332 * the error. The result of [onError] is handled exactly the same as for
333 * [then]'s [onError].
334 * 344 *
335 * If [test] returns false, the exception is not handled by [onError], but is 345 * If this future completes with an error,
336 * thrown unmodified, thus forwarding it to `f`. 346 * then [test] is first called with the error value.
337 * 347 *
338 * If [test] is omitted, it defaults to a function that always returns true. 348 * If `test` returns false, the exception is not handled by this `catchError`,
349 * and the returned future completes with the same error and stack trace
350 * as this future.
351 *
352 * If `test` returns `true`,
353 * [onError] is called with the error and possibly stack trace,
354 * and the returned future is completed with the result of this call
355 * in exactly the same way as for [then]'s `onError`.
356 *
357 * If `test` is omitted, it defaults to a function that always returns true.
358 * The `test` function should not throw, but if it does, it is handled as
359 * if the the `onError` function had thrown.
339 * 360 *
340 * Example: 361 * Example:
341 * 362 *
342 * foo 363 * foo
343 * .catchError(..., test: (e) => e is ArgumentError) 364 * .catchError(..., test: (e) => e is ArgumentError)
344 * .catchError(..., test: (e) => e is NoSuchMethodError) 365 * .catchError(..., test: (e) => e is NoSuchMethodError)
345 * .then((v) { ... }); 366 * .then((v) { ... });
346 * 367 *
347 * This method is equivalent to: 368 * This method is equivalent to:
348 * 369 *
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 * theFuture.catchError(thisCompleter.completeError); 591 * theFuture.catchError(thisCompleter.completeError);
571 * 592 *
572 */ 593 */
573 void completeError(Object error, [StackTrace stackTrace]); 594 void completeError(Object error, [StackTrace stackTrace]);
574 595
575 /** 596 /**
576 * Whether the future has been completed. 597 * Whether the future has been completed.
577 */ 598 */
578 bool get isCompleted; 599 bool get isCompleted;
579 } 600 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698