timed<R> function

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

Implementation

R timed<R>(R block(), {dynamic result(R result, Duration time)?}) {
  result ??= (R result, Duration time) {};

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