Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9f70bc7

Browse files
lrhnCommit Queue
authored and
Commit Queue
committed
Add Future.syncValue(T value) constructor.
There is currently no way to create a future with a specific value that doesn't accept a `FutureOr` argument, and tries to flatten it. The `Future.syncValue(T value)` constructor does not accept a `Future<T>`, which makes it easier to control the type and argument. It may also be more efficient than constructors which need to first check if the argument is a `Future<T>` (and `_Future<T>`) first. You shouldn't create a `Future<Future<Object>>`, but you might for testing, and this constructor makes that easier too. (It should really be called `Future.value`, but that name was taken by what should probably have been `Future.new`.) See https://dartbug.com/59814 for performance considerations. CoreLibraryReviewExempt: No platform specific code or new functionality. Change-Id: Ib5728055a06b20433dcd448ca7ad1baa54ec681c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/451022 Reviewed-by: Nate Bosch <nbosch@google.com> Commit-Queue: Lasse Nielsen <lrn@google.com>
1 parent 4f8af89 commit 9f70bc7

File tree

3 files changed

+181
-125
lines changed

3 files changed

+181
-125
lines changed

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ instead.
9090

9191
### Libraries
9292

93+
#### `dart:async`
94+
95+
- Added `Future.syncValue` constructor for creating a future with a
96+
known value. Unlike `Future.value`, it does not allow an asynchronous
97+
`Future<T>` as the value of a new `Future<T>`.
98+
9399
#### `dart:core`
94100

95101
- **Breaking Change** [#61392][]: The `Uri.parseIPv4Address` function

‎sdk/lib/async/future.dart‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,21 +294,31 @@ abstract interface class Future<T> {
294294
return result;
295295
}
296296

297-
/// Returns a future containing the result of immediately calling
298-
/// [computation].
297+
/// The result of calling [computation] as a future.
299298
///
300-
/// If calling [computation] throws, the returned future is completed with the
301-
/// error.
299+
/// Mainly used when working with functions returning a `FutureOr<T>`,
300+
/// or non-async functions in a non-`async` asynchronous function,
301+
/// where you want to capture any error in a future.
302+
///
303+
/// If calling [computation] throws, the returned future is completed
304+
/// with the error.
302305
///
303306
/// If calling [computation] returns a `Future<T>`, that future is returned.
304307
///
305-
/// If calling [computation] returns a non-future value,
306-
/// a future is returned which has been completed with that value.
308+
/// If calling [computation] returns a non-`Future<T>` value,
309+
/// a future is created which has been completed with that value.
307310
///
308311
/// Example:
309312
/// ```dart
310-
/// final result = await Future<int>.sync(() => 12);
313+
/// Future<int> asyncAdd(
314+
/// FutureOr<int> Function() a,
315+
/// FutureOr<int> Function() b,
316+
/// ) =>
317+
/// (Future.sync(a), Future.sync(b)).wait.then((ab) => ab.1ドル + ab.2ドル);
311318
/// ```
319+
///
320+
/// To create a future with a known value, use [Future.syncValue] instead,
321+
/// as `Future.syncValue(12)`.
312322
factory Future.sync(FutureOr<T> computation()) {
313323
FutureOr<T> result;
314324
try {
@@ -320,6 +330,17 @@ abstract interface class Future<T> {
320330
return result is Future<T> ? result : _Future<T>.value(result);
321331
}
322332

333+
/// Creates a future completed with [value].
334+
///
335+
/// The future is guaranteed to not have an error.
336+
///
337+
/// If a synchronous computation can throw,
338+
/// rather than doing `Future.syncValue(computation())` and wrapping that in
339+
/// a `try`/`catch`, you can use [Future.sync] which catches an error
340+
/// into its returned future, as `Future.sync(() => computation())`.
341+
@Since("3.10")
342+
factory Future.syncValue(T value) => _Future<T>().._setValue(value);
343+
323344
/// Creates a future completed with [value].
324345
///
325346
/// If [value] is a future, the created future waits for the

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /