fromAsyncCallback<R, S> static method

Future<Result<R, S>> fromAsyncCallback<R, S>(
  1. Future<R> future(), {
  2. required S error(
    1. dynamic e,
    2. StackTrace s
    ),
})

Create a Result from a callback that returns a Future.

If future completes successfully then Result will contain its value, and if the future fails then its error will be contained in Result

Implementation

static Future<Result<R, S>> fromAsyncCallback<R, S>(
  Future<R> Function() future, {
  required S Function(dynamic e, StackTrace s) error,
}) async {
  try {
    return Result<R, S>.ok(await future.call());
  } catch (e, s) {
    return Result<R, S>.err(error(e, s));
  }
}