map<N> method

AsyncSnapshot<N> map<N>(
  1. N fn(
    1. T
    )
)

Transforms the snapshot by applying a function to the Ok value, similar to Iterable.map.

fn - The function to apply to the Ok value. Returns a new AsyncSnapshot with the transformed value.

Example:

ok(21).map((x) => x * 2); // ok(42)
err<int>(Exception()).map((x) => x * 2); // err(Exception())
pending<int>().map((x) => x * 2); // pending()

Implementation

AsyncSnapshot<N> map<N>(N Function(T) fn) {
  if (isErr()) {
    return err<N>(error!, stackTrace);
  }

  if (isPending()) {
    return pending<N>();
  }

  return ok(fn(data as T));
}