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

Unified Diff: lib/coreimpl/future_implementation.dart

Issue 10958021: If transformException() returns a future, act like chain(). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Refactor out some common code. Created 8 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/core/future.dart ('k') | tests/corelib/future_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/coreimpl/future_implementation.dart
diff --git a/lib/coreimpl/future_implementation.dart b/lib/coreimpl/future_implementation.dart
index 15e4f2a0f2721f3ca5fa1ae679122b1d9e4e2a49..4146397680a4a4e44fbd6e06bd87bdf7a032ba3c 100644
--- a/lib/coreimpl/future_implementation.dart
+++ b/lib/coreimpl/future_implementation.dart
@@ -172,10 +172,7 @@ class FutureImpl<T> implements Future<T> {
Future transform(Function transformation) {
final completer = new Completer();
- handleException((e) {
- completer.completeException(e, stackTrace);
- return true;
- });
+ _forwardException(this, completer);
then((v) {
var transformed = null;
@@ -193,10 +190,8 @@ class FutureImpl<T> implements Future<T> {
Future chain(Function transformation) {
final completer = new Completer();
- handleException((e) {
- completer.completeException(e, this.stackTrace);
- return true;
- });
+
+ _forwardException(this, completer);
then((v) {
var future = null;
try {
@@ -205,11 +200,8 @@ class FutureImpl<T> implements Future<T> {
completer.completeException(ex, stackTrace);
return;
}
- future.handleException((e) {
- completer.completeException(e, future.stackTrace);
- return true;
- });
- future.then((b) => completer.complete(b));
+
+ _forward(future, completer);
});
return completer.future;
}
@@ -219,7 +211,15 @@ class FutureImpl<T> implements Future<T> {
handleException((ex) {
try {
- completer.complete(transformation(ex));
+ final result = transformation(ex);
+
+ // If the transformation itself returns a future, then we will
+ // complete to what that completes to.
+ if (result is Future) {
+ _forward(result, completer);
+ } else {
+ completer.complete(result);
+ }
} catch (innerException, stackTrace) {
completer.completeException(innerException, stackTrace);
}
@@ -230,6 +230,24 @@ class FutureImpl<T> implements Future<T> {
return completer.future;
}
+
+ /**
+ * Forwards the success or error completion from [future] to [completer].
+ */
+ _forward(Future future, Completer completer) {
+ _forwardException(future, completer);
+ future.then(completer.complete);
+ }
+
+ /**
+ * Forwards the exception completion from [future] to [completer].
+ */
+ _forwardException(Future future, Completer completer) {
+ future.handleException((e) {
+ completer.completeException(e, future.stackTrace);
+ return true;
+ });
+ }
}
class CompleterImpl<T> implements Completer<T> {
« no previous file with comments | « lib/core/future.dart ('k') | tests/corelib/future_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698