-
Notifications
You must be signed in to change notification settings - Fork 1.7k
proposal: avoid_returning_futureor_from_async_functions
- Avoid returning FutureOr from async functions
#59180
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Makes sense! Perhaps the lint could be even a bit stronger: Flag every function with an (Aside: The case The point is that those functions will return a future ( It is possible that such a return type is required with an instance member of a class or mixin because of the signatures of members that it is overriding and/or is overridden by, but this is probably rare enough to justify the need for an |
The fact that it can also interact badly with type inference is not the primary reason to not return a My recommendation (which has staid constant since the introduction of I know someone might want an API that can get a value synchronously or asynchronously, to save time on awaiting a future. If always awaiting the future is a pain-point for your API, you may want to have a more specialized API, like bool get hasValue; // Whether the [value] getter works.
V get value; // Throws if no value.
Future<V> waitForValue(); // Waits until [hasValue], then provides it. instead of forcing the user to do For The type problem here can't only happen for Future<T> churn<T>(FutureOr<T> value) async => await value; with a The issue with The real issue, the one I think we should warn about, is that the return type ends up being If you really, really have a good reason for a |
If that would catch this case and more, should we change this request to be just that then? |
I think the occurrence of nested future types (anywhere, not just as return types) is a completely different topic than using a return type on an |
avoid_returning_futureor_from_async_functions
Description
Avoid returning
FutureOr<T>
fromasync
functions.Details
Returning
FutureOr<T>
fromasync
methods may result inFuture
s that cannot be awaited for someT
s.Kind
Does this enforce style advice? Guard against errors? Other?
Bad Examples
Good Examples
Discussion
I filed #52621 after catching a bug in my code where a
Future
I expected to be awaited was not.A contrived example looks something like this. There are no errors in this code and it appeared as if everything was correctly awaited but it prints
FINISHED
beforeDOING WORK
.My understanding (from discussion in the linked issue) is that in
FutureOr<T> run2<T>() async
theT
isFuture<void>
and the returned value isFutureOr<Future<void>>
. Theawait runner.run()
is only awaiting the outerFuture
(created byrun2
beingasync
) and it doesn't actually await the work.Using
Future<T>
instead ofFutureOr<T>
in the return types avoids the issue (and in several other issues I've found this advice generally being given).I don't know how useful this would be to others. I think part of my issue was that I thought
await
would always unroll all futures (so the result of awaiting something would never be aFuture
), but it turns out that was incorrect. It's possible this is a Danny problem but I though it was worth filing for discussion.The text was updated successfully, but these errors were encountered: