from<T extends Object?> static method

Future<AsyncPhase<T>> from<T extends Object?>(
  1. FutureOr<T> func(
    1. {required T? fallbackData,
    2. void onError(
      1. Object,
      2. StackTrace

    A method that runs an asynchronous function and returns either AsyncComplete with the function result as data or AsyncError with the error information, depending on whether or not the function completed successfully.

    If the asynchronous function resulted in an error, the fallbackData value is used as the data of AsyncError.

    The onError callback is called on error. This may be useful for logging.


    static Future<AsyncPhase<T>> from<T extends Object?>(
      FutureOr<T> Function() func, {
      // The type must not be `T` because it can't accept a value of type
      // `T?` although it accepts `null` if the generic type is nullable.
      required T? fallbackData,
      void Function(Object, StackTrace)? onError,
    }) async {
      try {
        final data = await func();
        return AsyncComplete(data);
        // ignore: avoid_catches_without_on_clauses
      } catch (e, s) {
        onError?.call(e, s);
        return AsyncError<T>(data: fallbackData, error: e, stackTrace: s);