flatMap<A, B> function

Task<B> Function(Task<A> task) flatMap<A, B>(
  1. Task<B> fn(
    1. A
    )
)

Flat-maps over a Task by applying the provided function fn to its result.

Given a function fn that takes a value of type A and returns a Task of type B, and a Task that will eventually produce a value of type A, this function returns a new Task that will eventually produce a value of type B.

Example:

Task<int> sampleTask = Task(() => Future.value(5));
Task<String> flatMappedTask = flatMap<int, String>((int value) => Task(() => Future.value('Value is: $value')))(sampleTask);

Implementation

Task<B> Function(Task<A> task) flatMap<A, B>(Task<B> Function(A) fn) =>
    (Task<A> task) =>
        Task<B>(() async => (await (fn(await task.task())).task()));