OLD | NEW |
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 Loading... |
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 accept either one argument or two arguments. |
| 304 * If `onError` accepts two arguments, |
| 305 * it is called with both the error and the stack trace, |
| 306 * otherwise it is called with just the error object. |
304 * | 307 * |
305 * If the invoked callback returns a [Future] `f2` then `f` and `f2` are | 308 * Returns a new [Future] |
306 * chained. That is, `f` is completed with the completion value of `f2`. | 309 * which is completed with the result of the call to `onValue` |
| 310 * (if this future completes with a value) |
| 311 * or to `onError` (if this future completes with an error). |
307 * | 312 * |
308 * The [onError] callback must be of type `void onError(error)` or | 313 * If the invoked callback throws, |
309 * `void onError(error, StackTrace stackTrace)`. If [onError] accepts | 314 * the returned future is completed with the thrown error |
310 * two arguments it is called with the stack trace (which could be `null` if | 315 * and a stack trace for the error. |
311 * the stream itself received an error without stack trace). | 316 * In the case of `onError`, |
312 * Otherwise it is called with just the error object. | 317 * if the exception thrown is the same as the argument to `onError`, |
| 318 * the throw is considered a rethrow, |
| 319 * and the original stack trace is used instead. |
| 320 * |
| 321 * If the callback returns a [Future], |
| 322 * the future returned by `then` will be completed with |
| 323 * the same result of the future returned by the callback. |
313 * | 324 * |
314 * If [onError] is not given it forwards the error to `f`. | 325 * If [onError] is not given it forwards the error to `f`. |
315 * | 326 * |
316 * In most cases, it is more readable to use [catchError] separately, possibly | 327 * 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 | 328 * with a `test` parameter, instead of handling both value and error in a |
318 * single [then] call. | 329 * single [then] call. |
319 */ | 330 */ |
320 Future then(onValue(T value), { Function onError }); | 331 Future then(onValue(T value), { Function onError }); |
321 | 332 |
322 /** | 333 /** |
323 * Handles errors emitted by this [Future]. | 334 * Handles errors emitted by this [Future]. |
324 * | 335 * |
325 * Returns a new [Future] `f`. | 336 * This is the asynchronous equivalent of a "catch" block. |
326 * | 337 * |
327 * When [this] completes with a value, the value is forwarded to `f` | 338 * Returns a new [Future] that will be completed with either the result of |
328 * unmodified. That is, `f` completes with the same value. | 339 * this future or the result of calling the `onError` callback. |
329 * | 340 * |
330 * When [this] completes with an error, [test] is called with the | 341 * If this future completes with a value, |
331 * error's value. If the invocation returns [true], [onError] is called with | 342 * 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 * | 343 * |
335 * If [test] returns false, the exception is not handled by [onError], but is | 344 * If this future completes with an error, |
336 * thrown unmodified, thus forwarding it to `f`. | 345 * then [test] is first called with the error value. |
337 * | 346 * |
338 * If [test] is omitted, it defaults to a function that always returns true. | 347 * If `test` returns false, the exception is not handled by this `catchError`, |
| 348 * and the returned future completes with the same error and stack trace |
| 349 * as this future. |
| 350 * |
| 351 * If `test` returns `true`, |
| 352 * [onError] is called with the error and possibly stack trace, |
| 353 * and the returned future is completed with the result of this call |
| 354 * in exactly the same way as for [then]'s `onError`. |
| 355 * |
| 356 * If `test` is omitted, it defaults to a function that always returns true. |
| 357 * The `test` function should not throw, but if it does, it is handled as |
| 358 * if the the `onError` function had thrown. |
339 * | 359 * |
340 * Example: | 360 * Example: |
341 * | 361 * |
342 * foo | 362 * foo |
343 * .catchError(..., test: (e) => e is ArgumentError) | 363 * .catchError(..., test: (e) => e is ArgumentError) |
344 * .catchError(..., test: (e) => e is NoSuchMethodError) | 364 * .catchError(..., test: (e) => e is NoSuchMethodError) |
345 * .then((v) { ... }); | 365 * .then((v) { ... }); |
346 * | 366 * |
347 * This method is equivalent to: | 367 * This method is equivalent to: |
348 * | 368 * |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 * theFuture.catchError(thisCompleter.completeError); | 590 * theFuture.catchError(thisCompleter.completeError); |
571 * | 591 * |
572 */ | 592 */ |
573 void completeError(Object error, [StackTrace stackTrace]); | 593 void completeError(Object error, [StackTrace stackTrace]); |
574 | 594 |
575 /** | 595 /** |
576 * Whether the future has been completed. | 596 * Whether the future has been completed. |
577 */ | 597 */ |
578 bool get isCompleted; | 598 bool get isCompleted; |
579 } | 599 } |
OLD | NEW |