use<R> method

Future<R> use<R>(
  1. Future<R> action(
    1. T resource
    )
)

Borrows a resource for action, returning it to the pool afterward even if action throws. The usual entry point — it pairs acquire/release so a resource can never leak. Throws StateError if the pool is closed. Audited: 2026-06-12 11:26 EDT

Implementation

Future<R> use<R>(Future<R> Function(T resource) action) async {
  final T resource = await acquire();
  try {
    return await action(resource);
  } finally {
    release(resource);
  }
}