Chromium Code Reviews| Index: utils/pub/utils.dart |
| diff --git a/utils/pub/utils.dart b/utils/pub/utils.dart |
| index 2a800fa0ce59bd7d67e824321d220fcc218fbd20..ffde5622bb28cc31f9ab3255b2b001539887deb3 100644 |
| --- a/utils/pub/utils.dart |
| +++ b/utils/pub/utils.dart |
| @@ -40,12 +40,6 @@ String padRight(String source, int length) { |
| return result.toString(); |
| } |
| -/// Runs [fn] after [future] completes, whether it completes successfully or |
| -/// not. Essentially an asynchronous `finally` block. |
| -always(Future future, fn()) { |
| - future.catchError((_) {}).then((_) => fn()); |
| -} |
| - |
|
Bob Nystrom
2013/01/09 16:49:23
Yay!
|
| /// Flattens nested lists inside an iterable into a single list containing only |
| /// non-list elements. |
| List flatten(Iterable nested) { |
| @@ -124,6 +118,25 @@ void chainToCompleter(Future future, Completer completer) { |
| onError: (e) => completer.completeError(e.error, e.stackTrace)); |
| } |
| +/// Like [Future.whenComplete], except that [action] may return a [Future]. If |
| +/// it does, the returned [Future] won't complete until [action]'s [Future] |
| +/// completes. |
| +/// |
| +/// The returned [Future] always has the same value as [future]. |
|
Bob Nystrom
2013/01/09 16:49:23
Since this is how then() and catchError() work, it
|
| +Future asyncWhenComplete(Future future, Future action()) { |
| + Future futurify(Future future) { |
| + if (future != null) return future; |
| + return new Future.immediate(null); |
| + } |
| + |
| + return future.then((result) => futurify(action()).then((_) => result), |
| + onError: (e) { |
| + return futurify(action()).then((_) { |
| + throw e; |
| + }); |
| + }); |
| +} |
| + |
| // TODO(nweiz): unify the following functions with the utility functions in |
| // pkg/http. |
| @@ -194,3 +207,16 @@ getRealError(error) { |
| return error; |
| } |
| + |
| +// TODO(rnystrom): Remove this when #7781 is fixed. |
|
Bob Nystrom
2013/01/09 16:49:23
"nweiz", not that it really matters. :)
|
| +/// When an error is rethrown in an async callback, you can end up with nested |
| +/// AsyncErrors. This unwraps them to find the real originating stack trace. |
| +getRealStackTrace(error) { |
| + var trace = null; |
| + while (error is AsyncError) { |
| + trace = error.stackTrace; |
| + error = error.error; |
| + } |
| + |
| + return trace; |
| +} |