run<T> method

Future<T> run<T>(
  1. Future<T> task()
)

Implementation

Future<T> run<T>(Future<T> Function() task) async {
  if (_active < max) {
    _active++;
  } else {
    final slot = Completer<void>();
    _waiting.add(slot);
    await slot
        .future; // the finishing task hands its slot over, still counted
  }
  try {
    return await task();
  } finally {
    if (_waiting.isNotEmpty) {
      _waiting.removeFirst().complete();
    } else {
      _active--;
    }
  }
}