asStream<T> static method

Stream<AsyncResult<T>> asStream<T>(
  1. Future<T> fn()
)

Wraps an async function call in a try-catch block. Returns a Stream of AsyncResults.

The first result is always Loading.

If the function call succeeds, the second result is Success. If the function call throws, the second result is Failure.

Implementation

static Stream<AsyncResult<T>> asStream<T>(Future<T> Function() fn) async* {
  yield Async<T>.loading();
  try {
    yield Async.success(await fn());
  } catch (e, s) {
    yield Async.failure(e, s);
  }
}