-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect behavior of return in async function. #39204
Comments
Another reason awaiting is important is code like: Future<int> foo() async {
try {
return Future<int>.error("gotcha");
} finally {
return 0;
}
} The issue here is that if the returned future is not awaited at the return statement, it will be silently discarded and the error becomes uncaught instead of thrown. |
It's not clear to me what the concrete actions are to resolve this. Can we describe the specific change we are hoping for in the implementations? Should we consider pushing on dart-lang/language#870 to fix the ambiguity? |
Duplicate of #44395 |
The Dart 2 behavior for returns in async functions is to await a future if it's a future of the flattened return type.
That is:
will evaluate
someExpression
with context typeFutureOr<int>
, and if it is aFuture<int>
, it is awaited before returning its value.That it's awaited and not passed directly to a completer (an implementation detail) is important in the case where the return is inside a
try
/catch
an you try to return a future containing an error.Example:
See #22730 for history of the spec change.
The text was updated successfully, but these errors were encountered: