withResource<T> method

Future<T> withResource<T>(
  1. FutureOr<T> callback()
)

Requests a resource for the duration of callback, which may return a Future.

The return value of callback is piped to the returned Future.

Implementation

Future<T> withResource<T>(FutureOr<T> Function() callback) async {
  if (isClosed) {
    throw StateError('withResource() may not be called on a closed Pool.');
  }

  var resource = await request();
  try {
    return await callback();
  } finally {
    resource.release();
  }
}