timedAsync<R> function

Future<R> timedAsync<R>(
  1. FutureOr<R> block(), {
  2. dynamic result(
    1. R result,
    2. Duration time
    )?,
})

Implementation

Future<R> timedAsync<R>(FutureOr<R> block(),
    {dynamic result(R result, Duration time)?}) async {
  result ??= (R result, Duration time) {
    print("Duration: $time");
  };

  final start = DateTime.now();
  R r = await block();
  final duration = DateTime.now().difference(start);
  final handled = result(r, duration);
  return handled is R ? handled : r;
}