Chromium Code Reviews| Index: lib/src/result_future.dart |
| diff --git a/lib/src/result_future.dart b/lib/src/result_future.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..39f0f840d0543a4a3d12e5aa1d79ac0a6a82f909 |
| --- /dev/null |
| +++ b/lib/src/result_future.dart |
| @@ -0,0 +1,38 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library async.result_future; |
| + |
| +import 'dart:async'; |
| + |
| +import '../result.dart'; |
| +import 'delegate/future.dart'; |
| + |
| +/// A [Future] wrapper that provides synchronous access to the result of the |
| +/// wrapped [Future] once it's completed. |
| +class ResultFuture<T> extends DelegatingFuture<T> { |
|
Lasse Reichstein Nielsen
2015/07/08 11:01:56
I think I'd prefer if this was not a Future itself
nweiz
2015/07/09 01:18:38
This is designed for the use-case of a class that
Lasse Reichstein Nielsen
2015/07/09 11:18:20
I can see the argument - whether a future is compl
nweiz
2015/07/13 20:05:52
I'd be okay with changing this to just have an [is
|
| + /// The wrapped [Future]. |
| + Future<T> _future; |
|
Lasse Reichstein Nielsen
2015/07/09 11:18:20
I don't think this field is used at all.
nweiz
2015/07/13 20:05:52
Done.
|
| + |
| + /// The result of the wrapped [Future], if it's completed. |
| + /// |
| + /// If it hasn't completed yet, this will be `null`. |
| + Result<T> get result => _result; |
| + Result<T> _result; |
| + |
| + factory ResultFuture(Future<T> future) { |
| + var resultFuture; |
| + resultFuture = new ResultFuture._(future.then((value) { |
| + resultFuture._result = new Result.value(value); |
| + return value; |
| + }).catchError((error, stackTrace) { |
| + resultFuture._result = new Result.error(error, stackTrace); |
| + throw error; |
| + })); |
|
Lasse Reichstein Nielsen
2015/07/08 11:01:57
The `Result.capture(Future future)` can handle thi
nweiz
2015/07/09 01:18:38
Done.
|
| + return resultFuture; |
| + } |
| + |
| + ResultFuture._(Future<T> future) |
| + : super(future); |
| +} |