map<A, B> function

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

Maps over a Task by applying the provided function fn to its result.

Given a function fn that transforms a value of type A to a value 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> mappedTask = map<int, String>((int value) => 'Value is: $value')(sampleTask);

Implementation

Task<B> Function(Task<A> task) map<A, B>(B Function(A) fn) {
  return (Task<A> task) {
    return Task<B>(() async => fn(await task.task()));
  };
}