asyncHandle<T> function

Future<T?> asyncHandle<T>(
  1. FutureOr<T?> task(), {
  2. T? alt(
    1. String msg
    )?,
  3. FutureOr<T?> ult(
    1. FutureOr<T?> condition,
    2. bool success
    )?,
})

Async Event Exception Handler

Implementation

Future<T?> asyncHandle<T>(
  FutureOr<T?> Function() task, {
  T? Function(String msg)? alt,
  FutureOr<T?> Function(FutureOr<T?> condition, bool success)? ult,
}) async {
  T? _alt(String e) => (alt ?? (e) => null)(e);
  FutureOr<T?> _ult(FutureOr<T?> c, bool s) => (ult ?? (c, s) async => await c)(c, s);
  try {
    return await _ult(await task(), true);
  } catch (e) {
    try {
      return _ult(_alt((e as Exception).msg), false);
    } catch (e) {
      return _ult(_alt(''), false);
    }
  }
}