fromThrowable<A, B> function

FutureOr<B> fromThrowable<A, B>(
  1. FutureOr<A> f(), {
  2. required B onSuccess(
    1. A a
    ),
  3. required B onError(
    1. dynamic error,
    2. StackTrace stackTrace
    ),
})

Implementation

FutureOr<B> fromThrowable<A, B>(
  FutureOr<A> Function() f, {
  required B Function(A a) onSuccess,
  required B Function(dynamic error, StackTrace stackTrace) onError,
}) {
  try {
    final a = f();
    if (a is Future) {
      return (a as Future<A>).then(onSuccess, onError: onError);
    }
    return onSuccess(a);
  } catch (err, stack) {
    return onError(err, stack);
  }
}