Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(542)

Unified Diff: lib/pool.dart

Issue 1853273002: Don't use async/await for Pool.withResource(). (Closed) Base URL: git@github.com:dart-lang/pool@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/pool.dart
diff --git a/lib/pool.dart b/lib/pool.dart
index f7bfd828b8891c1d6b5dd99dc403072583e2aeb1..deb53334b8036eb9bf115fac2a666ae566f2655f 100644
--- a/lib/pool.dart
+++ b/lib/pool.dart
@@ -106,18 +106,19 @@ class Pool {
/// Future.
///
/// The return value of [callback] is piped to the returned Future.
- Future/*<T>*/ withResource/*<T>*/(/*=T*/ callback()) async {
+ Future/*<T>*/ withResource/*<T>*/(/*=T*/ callback()) {
if (isClosed) {
throw new StateError(
"withResource() may not be called on a closed Pool.");
}
- var resource = await request();
- try {
- return await callback();
- } finally {
- resource.release();
- }
+ // We can't use async/await here because we need to start the request
+ // synchronously in case the pool is closed immediately afterwards. Async
+ // functions have an asynchronous gap between calling and running the body,
+ // and [close] could be called during that gap. See #3.
+ return request().then((resource) {
+ return new Future.sync(callback).whenComplete(resource.release);
+ });
}
/// Closes the pool so that no more resources are requested.
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698