Skip to content
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

Not an issue - you don't need to return await #6

Open
avioli opened this issue Apr 9, 2020 · 2 comments
Open

Not an issue - you don't need to return await #6

avioli opened this issue Apr 9, 2020 · 2 comments

Comments

@avioli
Copy link

avioli commented Apr 9, 2020

You don't need to return await ...:

Future<http.Response> httpGet(
String path, {
Map<String, String> queryParams,
}) async {
return await _safelyRun(() async {
var url = Uri.https(hostname, path, queryParams);
return await http.get(url, headers: headers);
});
}

Which could be safely rewritten to:

 Future<http.Response> httpGet( 
   String path, { 
   Map<String, String> queryParams, 
 }) { 
   return _safelyRun(() { 
     final url = Uri.https(hostname, path, queryParams); 
     return http.get(url, headers: headers); 
   }); 
 } 

The future chain will await by itself.

You only need to when in a try/catch block, since you need to get the future to resolve within your scope:

Future<http.Response> _safelyRun(Future<http.Response> method()) async {
try {
return await method();
} on SocketException {
throw NoNetworkError();
} on http.ClientException {
throw NetworkError();
} on TlsException {
throw NetworkError();
}
}

@safeforge
Copy link
Member

Sorry @avioli, this really went out of my radar - can you create a PR?

Thank you!

@avioli
Copy link
Author

avioli commented Aug 21, 2021

#16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants